[patterns] reconsidering how the created variable name binds

John Rose john.r.rose at oracle.com
Sat Dec 28 22:10:17 UTC 2019


On Dec 28, 2019, at 12:47 PM, John Rose <john.r.rose at oracle.com> wrote:
> 
>  static String v = "field";
>  public void test(CharSequence obj) {
>      if (!(obj instanceof String v)) {
>          // Report error. Rarely taken. 
>          throw stuff();
>      }
> 
>      System.out.println(v);
>    }
> 
> Here the if statement has no else clause. The fall-through path is where you go when the pattern doesn’t match. I changed String obj to be CharSequence to make it more realistic. (Does that lose the point of your example?)

P.S.  Also, there are other ways to deal with field shadowing,
without actually contracting the language to avoid puzzlers.
(Making a rule narrower to avoid bad use cases contracts
the language since it also rules out reasonable use cases, right?)
Instead of making examples like yours and mine illegal, it’s
reasonable to expect that IDEs or maybe the language compiler
can add warnings, if there’s truly a problem.  And that move also
pushes the problem out of the JLS proper and into tooling, where
the problem is easier to deal with, and induces fewer requirements
on an already over-constrained design problem.  In general, this is
why we don’t make the language force people to eat their broccoli.


More information about the amber-dev mailing list