如何返回多个值并将其分配给可变变量? [英] How do you return multiple values and assign them to mutable variables?

查看:89
本文介绍了如何返回多个值并将其分配给可变变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所拥有的.

This is what I have so far.

let Swap (left : int , right : int ) = (right, left)

let mutable x = 5
let mutable y = 10

let (newX, newY) = Swap(x, y) //<--this works

//none of these seem to work
//x, y <- Swap(x, y)
//(x, y) <- Swap(x, y)
//(x, y) <- Swap(x, y)
//do (x, y) = Swap(x, y)
//let (x, y) = Swap(x, y)
//do (x, y) <- Swap(x, y)
//let (x, y) <- Swap(x, y)

推荐答案

您不能;没有语法可以通过一次分配来更新多个可变变量".当然可以!

You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do

let newX, newY = Swap(x,y)
x <- newX
y <- newY

这篇关于如何返回多个值并将其分配给可变变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆