Search code examples
f#mutable

How do you return multiple values and assign them to mutable variables?


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)

Solution

  • 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