PM design question: Shadowing

Remi Forax forax at
Sat Nov 11 13:30:21 UTC 2017

It's a step too far, as you said the rules for shadowing are currently the same for all constructs, let's not try to break that unity. 

Your first example already exist with the current construct: 
//field i in scope 
if (o instanceof Integer) { 
Integer i = (Integer)o; 
System.out.println(i); // local variable 
} else { 
System.out.println(i); // field 

Furthermore, given that all IDEs colorize differently a local variable access vs a field access, from my experience, it's not a real issue apart for the very beginners. 


> De: "Gavin Bierman" <gavin.bierman at>
> À: "amber-spec-experts" <amber-spec-experts at>
> Envoyé: Vendredi 3 Novembre 2017 11:45:30
> Objet: PM design question: Shadowing

> Shadowing

> As mentioned in the previous email, Java currently has five constructs that
> declare fresh variables. All five declare variables under the same shadowing
> regime: (i) No shadowing of formal parameters, (ii) No shadowing of other
> locally declared variables, but (iii) shadowing permitted of fields. Thus we
> would expect pattern variables to shadow fields.

> But such a decision has some interesting consequences. For example, if we adopt
> flow-like scoping strategy (c or d in the previous email), then the following
> code has some subtle behaviour.
> // field i in scope
> switch (o) {
>     case Integer i : System.out.print(i); // shadows field i
>                      break;
>     case T t :       System.out.print(i); // field i
> }

> Is this too confusing?

> We could also consider allowing variables to shadow other variables when they
> are in scope and DU. For example, if we adopt scoping strategy (b) from the
> previous email - where the scope of a pattern variable is the entire enclosing
> statement - the following code would be allowed.
> if (o matches T t) {
>     // t in scope and DA
> } else {
>     // t in scope and DU
>     if (o1 matches Integer t) {
>         // Integer t shadows T t
>     }
> }
> Should we restrict this new notion of shadowing to pattern variables only, or
> change it for all variables in Java. Would this be a step too far?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the amber-spec-experts mailing list