Q:

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:

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.

A:

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.

