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?