首页 文章

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

提问于
浏览
9

使用Chris Smith的Programming 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

为什么?

1 回答

  • 10

    Edit: 以下答案对于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
    

    另一方面,为了捕获可变,捕获需要通过引用来完成,否则您将无法修改它 . 但这是不可能的,因为在前面提到的返回闭包并在其定义范围之外使用的情况下,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 . 它们是堆分配的(而不是堆栈分配的本地可变),因此只要闭包持有对它的引用,它就不会被释放 .

相关问题