Pattern variable and field shadowing
Brian Goetz
brian.goetz at oracle.com
Thu Nov 28 13:41:57 UTC 2019
We struggled with this one - we call it the “Swiss cheese” problem.
The current rules are cheese-friendly. We considered trying to rule it out but we were concerned that the cure would be more complex than was worth it.
Sent from my iPad
> On Nov 27, 2019, at 10:39 PM, Tagir Valeev <amaembo at gmail.com> wrote:
>
> Hello!
>
> Consider the following code:
>
> public class A {
> private String str;
>
> public void f(Object obj) {
> if (obj instanceof String str) {
> System.out.println(str.toLowerCase()); // str refers to
> pattern binding
> } else {
> System.out.println(str.toLowerCase()); // str refers to the field
> }
> }
> }
>
> I thought that such a code should be rejected by the compiler, as it's
> confusing and could be a source of very subtle bugs. However, I
> haven't found any explicit statement regarding this in the latest spec
> draft [1]. Could you please clarify whether such code is acceptable
> and point me to the relevant part of the spec draft. Thank you!
>
> With best regards,
> Tagir Valeev.
>
> [1] http://cr.openjdk.java.net/~gbierman/jep305/jep305-20191021/specs/patterns-instanceof-jls.html
More information about the amber-spec-experts
mailing list