Is there a way to have warnings for shadowing values in F# in Visual Studio?

前端 未结 2 430
萌比男神i
萌比男神i 2021-01-18 02:10

To me shadowing of existing values like described in:

Shadowing and Nested function
immutable in F#
f# duplicate definition
FSharp for fun and profit com

相关标签:
2条回答
  • 2021-01-18 02:53

    One place I use shadowing is when resolving an optional parameter to a default value if no value was supplied.

    member x.Foo(?myFlag: bool) =
        let myFlag = defaultArg myFlag false
        ...
    

    Also F# Interactive, the way it's implemented now, would be pretty much completely non-functional if we didn't have shadowing.

    0 讨论(0)
  • 2021-01-18 03:09

    Shadowing has pros and cons. I too have encountered bugs due to fat-fingered shadowing efforts. On the plus side, it can help keep your variable space clean as @JoelMueller pointed out.

    Shadowing bugs are fundamentally different than mutable variable bugs. They are of the typo variety. They are much easier to analyze: historical information loss is minimized to lexicographical context versus environmental context. That is, with shadowing, you can always cleanly trace the value of a binding through mental stack unrolling, whereas variable mutations create what are essentially gotos (jump to address).

    Practically, shadowing still eliminates whole classes of bugs. You won't encounter any "spooky actions from a distance". Namely, you won't run into issues with variables captured in closures or variables being modified in nested scopes relative to the current scope.

    0 讨论(0)
提交回复
热议问题