使用Chris编程F# 3.0中的一个例子
let invalidUseOfMutable() =
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;正如预期的那样,这一做法失败了:
error FS0407:可变变量'x‘以无效的方式使用。闭包不能捕获可变变量。
现在,将函数的主体剪切并粘贴到FSharp互动中:
let mutable x = 0
let incrementX() = x <- x + 1
incrementX()
x;;而且起作用了!
val it : int =1
为什么?
发布于 2013-05-12 21:41:02
编辑:下面的答案对于最高为3.x的F#是正确的。从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。它们是堆分配的(相对于堆栈分配的本地可变的),因此只要闭包保持对它的引用,它就不会被解除分配。
https://stackoverflow.com/questions/16509587
复制相似问题