F# value shadowing - is it possible to disable val

2019-05-16 16:08发布

问题:

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

回答1:

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.



标签: f# shadowing