编辑:以下答案适用于F# 3.x及以下版本。从F# 4.0开始,如果需要,本地可变量会自动转换为ref
,因此OP的代码在所有情况下都可以成功编译。
简短回答:这不是由于fsi
,而是由于可变变量是全局的。
长回答:
对于正常(非可变)捕获,实现方式是将捕获的值复制到函数对象中,因此如果您返回此函数并在定义它的范围之外使用它,一切都可以正常工作。
let pureAddOne() =
let x = 1
let f y = x + y // the value 1 is copied into the function object
f
let g = pureAddOne()
g 3 // x is now out of scope, but its value has been copied and can be used
另一方面,为了捕获可变对象,需要通过引用进行捕获,否则将无法修改它。但这是不可能的,因为在前面提到的情况下,闭包被返回并在其定义范围之外使用时,可变对象也超出了范围并可能被释放。这就是最初限制的原因。
let mutableAddOne() =
let mutable x = 1
let f y = x <- x + y // x would be referenced, not copied
f
let g = mutableAddOne()
g 3 // x is now out of scope, so the reference is invalid!
// mutableAddOne doesn't compile, because if it did, then this would fail.
然而,如果可变对象是全局的,那么就不存在这样的作用域问题,编译器会接受它。这不仅适用于fsi
;如果您尝试使用fsc
编译以下程序,它也可以工作:
module Working
let mutable x = 1 // x is global, so it never goes out of scope
let mutableAddOne() =
let f y = x <- x + y // referencing a global. No problem!
f
let g = mutableAddOne()
g 3 // works as expected!
总之,正如kwingho所说,如果你想要一个捕获本地可变值的闭包,请使用ref
。它们是堆分配的(而不是栈分配的本地可变),因此只要闭包持有对它的引用,它就不会被释放。