简体   繁体   中英

Assignment operator in f#

i have seen in ruby as well powershell programming we can assign variables like a,b=b,a . it actually swaps the variable .

Is this possible in f# if so please guide me with some reference

Generally, F# doesn't allow variable re-assignment. Rather it favors immutable named values via let bindings. So, the following is not possible:

let a = 3
a = 4

Unless you explicitly mark a as mutable :

let mutable a = 3
a <- 4

However, F# does allow in most situations variable "shadowing". The only restriction to this is that it can not be done on top level modules. But, within a function, for example, the following works fine:

let f () =
    let a,b = 1,2
    let a,b = b,a //"swap"
    a,b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM