7

To me shadowing of existing values like described in:

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

seems to be going against the notion of immutability and type safety that makes F# so strong. Shadowing in F# works different than in C#. It just took me quite some time to find out that a bug in my code was due to unintentional shadowing of a name within the same scope. Is there a way to have compiler warnings for shadowing values in VS?

I know that in some cases it can be useful. for example for Checked Aritmetics .

Community
  • 1
  • 1
Goswin Rothenthal
  • 2,244
  • 1
  • 19
  • 32
  • 2
    Shadowing is useful, as pointed out here: http://infsharpmajor.wordpress.com/. You may want to explain what you think shadowing is, as it may be that your understanding is off, and clearing that up may solve your concern. – James Black Jun 24 '14 at 14:51
  • @Goswin: The [F# Power Tools](http://visualstudiogallery.msdn.microsoft.com/136b942e-9f2c-4c0b-8bac-86d774189cff) will highlight references, which can help with avoiding those kinds of bugs. – Daniel Jun 24 '14 at 15:24
  • @JamesBlack I edited my question to be clearer – Goswin Rothenthal Jun 24 '14 at 15:31
  • @Daniel Is there an explicit highlight for redefined names? that would be great !! – Goswin Rothenthal Jun 24 '14 at 15:34
  • 1
    When you hover over a value it will highlight all references to that value. That could help you distinguish it from other values with the same name. You can see a demo [here](http://fsprojects.github.io/VisualFSharpPowerTools/highlightusage.html), although it doesn't show shadowing unfortunately. – Daniel Jun 24 '14 at 15:40

2 Answers2

5

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.

Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
3

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.

Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136