Assignment operator in f#

后端 未结 1 2018
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 07:29

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

相关标签:
1条回答
  • 2020-12-11 08:22

    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
    
    0 讨论(0)
提交回复
热议问题