Fsharp: Variable
Variable (constants)
all variables are constants (value cannot change), except if declared with keyword mutable.
let xs = "abc" printfn "%s" xs // abc
// multiple variables let xx, yy = (3,4) printfn "%d %d" xx yy // 3 4
Mutable variable
let mutable xx = "aa" printfn "%s" xx // aa // change the variable value xx <- "bb" printfn "%s" xx // bb
Named Literal (Declare Compile Time Literal)
- Variables can be declared as compile time constants.
- Variable without mutable declared are always constants, but at run time.
precede the let line by
[<Literal>]
// compile time constant [<Literal>] let Xy = 3 + 4 printfn "%d" Xy // 7 // normal. (run-time constant) let xx = 3 + 4 printfn "%d" xx // 7
🟢 TIP: by convention, compile-time constants should start with Capital letter, because in pattern matching, lower case names are always treated as matched patterns.