Search code examples
f#functional-programmingshadowing

Error FS0037 sometimes, very confusing


If I write the following F# code, the compiler issues an error.

let a = 123
let a = 123

The error produced is:

error FS0037: Duplicate definition of value 'a'

If I write the same code in a function like this:

let fctn = 
    let a =123
    let a =123
    a

it doesn't produce any error.

I don't understand the difference. Can anyone please explain?

Edit : first code I write in module level.


Solution

  • I agree this is confusing. The problem is that let behaves differently when it is used as a local variable (within a function) and when it is used as a global definition (within a module).

    Global definitions (in a module) are compiled as static members of a static class and so a name can be used only once. This means that top-level use of:

    let a = 10
    let a = 11
    

    ... is an error, because F# would have to produce two static members of the same name.

    Local definitions (inside a function or some other nested scope) are compiled to Common IL and the variable name essentially disappears (the IL uses the stack instead). In this case, F# allows you to shadow variables, that is, you can hide a previous variable by re-using an existing name. This can be inside a function, or even just a do block inside a module, type or other function:

    do
      let a = 10
      let a = 11
      ()
    

    This is a bit confusing, because variable shadowing only works inside local scopes but not at the top level. It makes sense when you know how things are compiled though.

    As an aside, while IL allows overloads of members by the same name, such overloads cannot be defined at module level in F#. Instead, you'd need to define them specifically as static member on a class (type in F#).