F# 为什么Fsharp Interactive允许闭包捕获可变变量?

F# 为什么Fsharp Interactive允许闭包捕获可变变量?,f#,closures,mutable,f#-interactive,F#,Closures,Mutable,F# Interactive,使用Chris Smith的编程F#3.0中的一个示例: let invalidUseOfMutable()= 设可变x=0 让incrementX()=x编辑:以下答案对于F#到3.x是正确的。从F#4.0开始,如果需要,本地变量会自动转换为refs,因此OP的代码在所有情况下都会成功编译 简短回答:这不是因为fsi,而是因为可变是全局的 长答覆: 对于正常(不可变)捕获,在实现方面,捕获的值会复制到函数对象中,因此,如果您返回此函数并在定义它的范围之外使用它,则一切正常 let pureA

使用Chris Smith的编程F#3.0中的一个示例:

let invalidUseOfMutable()=
设可变x=0

让incrementX()=x编辑:以下答案对于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
另一方面,为了捕获可变表,需要通过引用进行捕获,否则您将无法修改它。但这是不可能的,因为在前面提到的闭包被返回并在其定义范围之外使用的情况下,可变表也在范围之外并且可能被解除分配。这就是最初限制的原因

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.

总之,正如kwingho所说,如果您想要一个捕获本地可变值的闭包,请使用
ref
。它们是堆分配的(与堆栈分配的本地可变项相反),因此只要闭包持有对它的引用,它就不会被释放。

Hi,当您只运行主体代码时,它不会生成闭包。用法:让x=ref 0;让递增x()=x:=!x+1;请参阅闭包:请注意,编译器本身也不反对函数体。一些要点(对我来说):“使用
ref
…[用于捕获局部可变]…”和“F#(从4.0)自动将
mutable
转换为
ref
。因此代码总是从F#4.0开始工作。
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.
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!