Ole*_*nov 3 f# closures mutable ref
F#中有些模拟?就像是
let f () =
let mutable static a = 0
...
Run Code Online (Sandbox Code Playgroud)
?
如果你desugar let f () = ...来let f = fun () -> ...,你可以把声明a的定义内f,但功能开始之前.这将使函数a在保持a本地的同时关闭f.这个问题是您可能无法关闭可变变量,因此您需要使用ref:
let f =
let a = ref 0
fun () ->
....
Run Code Online (Sandbox Code Playgroud)