使用Chris Smith的编程F#3.0 中的示例:
let invalidUseOfMutable() =
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;
这按预期失败:
错误FS0407:可变变量'x'以无效方式使用。 闭包不能捕获可变变量。
现在将函数体剪切并粘贴到FSharp Interactive中:
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;
它有效!
val it:int = 1
为什么?
答案 0 :(得分:10)
编辑:以下答案对于F#最高为3.x是正确的。从F#4.0开始,如果需要,本地变量将自动转换为ref
,因此OP的代码实际上将在所有情况下成功编译。
简短回答:这不是因为fsi
,而是因为mutable是全局的。
答案很长:
对于正常(非可变)捕获,实现方式将捕获的值复制到函数对象中,这样如果返回此函数并在定义范围之外使用它,一切正常
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
另一方面,为了捕获可变,捕获需要通过引用来完成,否则你将无法修改它。但这是不可能的,因为在前面提到的返回闭包并在其定义范围之外使用的情况下,mutable也超出了范围并可能被释放。这是初始限制的原因。
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.
但是,如果mutable是全局的,那么就没有这样的范围问题,并且编译器会接受它。这不只是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
。它们是堆分配的(而不是堆栈分配的本地可变),因此只要闭包持有对它的引用,它就不会被释放。