Questions tagged [shadowing]

In computer programming, shadowing occurs when a variable declared within a certain scope (decision block, method or inner class) has the same name as a variable declared in an outer scope. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

One of the first languages to introduce variable shadowing was ALGOL, which first introduced blocks to establish scopes. It was also permitted by many of the derivative programming languages including C++ and Java.

The C# language breaks this tradition, allowing variable shadowing between an inner and an outer class, and between a method and its containing class, but not between an if-block and its containing method, or between case statements in a switch block.

237 questions
9
votes
1 answer

G++ -Wshadow doesn't warn about static member shadowing

Once again I lost some hours because of mere stupidity which could have been recognized by the compiler. This is the source code in question: class f { static int mVar; int g(int x) { int mVar=3; return x+mVar; } }; int f::mVar = 1; The…
mxmlnkn
  • 1,887
  • 1
  • 19
  • 26
9
votes
2 answers

Shadowing and Nested function

I want to understand how the mechanism of Shadowing and Nested function work. For example: let func y = let dup y = y + y let z = dup y let dup y = let dup z = let y = y * z y let z = y y …
Neo
  • 888
  • 1
  • 7
  • 19
9
votes
6 answers

Why does shadowed variable evaluate to undefined when defined in outside scope?

Consider the following piece of code: