Tru*_*ill 9 f# closures mutable f#-interactive
使用Chris Smith的Programming F#3.0中的一个例子:
let invalidUseOfMutable() =
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;
Run Code Online (Sandbox Code Playgroud)
这按预期失败:
错误FS0407:可变变量'x'以无效方式使用.闭包不能捕获可变变量.
现在将函数体剪切并粘贴到FSharp Interactive中:
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;
Run Code Online (Sandbox Code Playgroud)
它的工作原理!
val it:int = 1
为什么?
Tar*_*mil 10
编辑:以下答案对于F#最高为3.x是正确的.从F#4.0开始,ref如果需要,本地变量将自动转换为s,因此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
Run Code Online (Sandbox Code Playgroud)
另一方面,为了捕获可变,捕获需要通过引用来完成,否则您将无法修改它.但这是不可能的,因为在前面提到的返回闭包并在其定义范围之外使用的情况下,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.
Run Code Online (Sandbox Code Playgroud)
但是,如果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!
Run Code Online (Sandbox Code Playgroud)
总而言之,正如kwingho所说,如果你想要一个捕获本地可变值的闭包,请使用a ref.它们是堆分配的(而不是堆栈分配的本地可变),因此只要闭包持有对它的引用,它就不会被释放.