3

I spoted a bug in my own code because of copy/paste. The same value name is shadowned by the copy/pasted in the same scope.

let func() =
    let a = 1
    let a = something_else
    ....

In C# I wont pass compile. is there a way to disable shadowing? at least within the same level of scope?

Thanks

sepp2k
  • 363,768
  • 54
  • 674
  • 675
casbby
  • 896
  • 7
  • 20

1 Answers1

7

You can't disable shadowing in F# -- it's an important language feature.

However, you can instruct the compiler to issue a warning or error which will help you catch cases of accidental shadowing.

In the project properties, add --warnon:1182 to the "Other flags" textbox (on the Build tab, under the platform target dropdown). This triggers a compiler warning when you accidentally shadow a variable and cause it not to be used anywhere. If you'd rather these cases cause compilation to fail, you can also add 1182 to the "Specific warnings" textbox under the "Treat warnings as errors" section of the Build tab.

Finally -- do install the Visual F# Power Tools extension. It provides additional syntax highlighting functionality and will indicate unused variables so they're easy to spot in your code.

Jack P.
  • 11,487
  • 1
  • 29
  • 34
  • 1
    Thanks jack. That is exactly what I was after. I also noticed F# power pack grays out the value. On the other do you mind elaborate in this post why is shadowing important to F#? I am not sure what workflow has to be done by shadowing. It seems all the immutability security can easily be undone by shadowing ( I am aware shadowing is not mutation) – casbby Apr 12 '15 at 23:40