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:
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
xvalue is compiled as a static field of themainmodule (represented as a class).I think you should be always able to see it in the watches window if you enter
Foo.main.x(whereFoois the namespace of your file – if you don’t provide namespace explicitly, this will be generated from the file name such asfoo.fsin 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 wherexis 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.