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
17
votes
3 answers

Defining a variable and its static equivalent in the same function

I don't understand how the following code works: #include "stdio.h" int main(void) { int i = 3; while(i--) { static int i = 100; i--, printf("%d\n", i); } return 0; } The code compiled with either Clang or GCC prints the…
Xatyrian
  • 1,364
  • 8
  • 26
16
votes
2 answers

In C++, what is the scope resolution ("order of precedence") for shadowed variable names?

In C++, what is the scope resolution ("order of precedence") for shadowed variable names? I can't seem to find a concise answer online. For example: #include int shadowed = 1; struct Foo { Foo() : shadowed(2) {} void bar(int…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
16
votes
10 answers

Is "this" shadowing a good idea?

The case of shadowing class variables is common in in Java. Eclipse will happily generate this code: public class TestClass { private int value; private String test; public TestClass(int value, String test) { super(); …
user232854
15
votes
2 answers

Weirdness calling str() to convert integer to string in Python 3?

Why is this giving me an error? >>> variable = str(21) Traceback (most recent call last): File "", line 1, in variable = str(21) TypeError: 'str' object is not callable
blueplastic
  • 201
  • 1
  • 2
  • 4
14
votes
2 answers

How does R know to use a function, if that functions name has been reassigned to a value?

I know it's good practice not to use names from the global namespace when naming variables, but what happens when you do this accidentally? I thought I would lose the previous object but R seems to have some trickery under the hood: print(sd) #>…
rdh
  • 1,035
  • 7
  • 11
12
votes
1 answer

`decltype` of generalized lambda capture inside body of a lambda - gcc vs clang

Consider the following code: #include int main() { auto l = [k = 0] { static_assert(std::is_same_v); }; } clang++ (10.x and trunk) happily compiles the code above. g++ (10.x and trunk) fails to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
12
votes
4 answers

How do I find where a variable is defined at runtime?

I've been using jQuery and YUI side-by-side with no issues until recently. Occasionally, inside of a callback for, say, a YUI button, $ will be shadowed by some other function (click for big version): and for the life of me, I cannot figure out why…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
11
votes
2 answers

C++: Declaration of parameter hides class member even with "this" keyword

I've recently started using "Warning Level 4" and treating warnings as errors when coding in C++. I'd like some more in-depth information about the following snippet: struct Foo { Foo(int size) //:size{ size } // <- More on this later. …
micka190
  • 742
  • 2
  • 8
  • 20
11
votes
3 answers

Shadowing vs. Setting value in F#

I was taught, that data, by default, is immutable in F#. When we reassign value to some variable, what really happens is that it rebinds the value of variable, but setting a new value is different thing. Rebinding is called Shadowing whilst setting…
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
11
votes
3 answers

Does a SFINAEd-out function shadows an explicitly imported overload from the base class

After having hitten problems with another design, I decided make make a wrapper class to add overloads to a some member functions of the base class if and only if viable overloads do not already exist in the base class. Basically, here is what I am…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
10
votes
1 answer

Force accessing of a def

Considering object A { def m(i: Int) = i val m = (i: Int) => i * 2 } one gets scala> A.m(2) : error: ambiguous reference to overloaded definition, both value m in object A of type => (Int) => Int and method m in object A of type (i:…
Debilski
  • 66,976
  • 12
  • 110
  • 133
9
votes
6 answers

In Java, if a child class shadows a static parent variable with an instance child variable, which variable will inherited methods use?

This is probably a bad thing to do, as discussed in Can parent and child class in Java have same instance variable?. (What if the parent variable name is changed? Then it will not be shadowed anymore.) However, I am still curious whether…
Anonymous
  • 3,334
  • 3
  • 35
  • 50
9
votes
2 answers

Shadowing Inherited Generic Interface Members in .NET: good, bad or ugly?

I know that shadowing members in class implementations can lead to situations where the "wrong" member can get called depending on how I have cast my instances, but with interfaces I don't see that this can be a problem and I find myself writing…
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
9
votes
2 answers

Scala: How can an import prevent finding an implicit value?

I could use suggestions debugging an implicit: I want to use the implicit, x: type T trait HasT { implicit def x: T = ... } But I also need a wildcard import from some package foo. I've tried two different ways of introducing both: class UseT…
stewSquared
  • 827
  • 5
  • 24
9
votes
4 answers

Why does an inner class method hide all the enclosing class methods with the same name?

Considering the java code below: class Enclosing { void method(){} void method(String str){} class Inner { void method(){} } } I am reading a book which tells me that Inner.method() will hide both versions of…
free6om
  • 438
  • 3
  • 10
1
2
3
15 16