开发者

F# variable out of context

In the following dummy code, if I set a break point at the last line, the variable x is not accessible in the debugger with:

开发者_StackOverflow中文版

the name x does not exist in the current context.

module main = 
    let x = 1
    printfn "%d" x
    1

But if I change the last line to 1|>ignore and set a break point there, I can see x = 1 in the debugger. How does F determine in the first case x is out of scope? Thanks.


In this context, the x value is compiled as a static field of the main module (represented as a class).

I think you should be always able to see it in the watches window if you enter Foo.main.x (where Foo is the namespace of your file - if you don't provide namespace explicitly, this will be generated from the file name such as foo.fs in this case).

Why do you see the variable if you add ignore? I'm not entirely sure - it is probably because the F# compiler sets the breakpoint to some place in the same class where x is placed (as a field). The lookup done by debugger follows the C# (.NET) rules, so it looks at the compiled code and not at the F# source code (because the F# integration doesn't provide its own resolver).

In general, you can assume that local variables can be viewed if you're inside a function where they are declared. Captured variables in a closure can be usually accessed using this (which gives you reference to the closure object), but this may depend on some compiler internals.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜