Ambiguity in Patterns that could reference both a field and type

Johannes Kuhn info at j-kuhn.de
Fri Jul 24 07:36:44 UTC 2020


While reading through the recent discussion amber-spec-experts, I 
remembered that Java has separate namespaces for fields and types (and 
methods for that matter).

Which could lead to some ambiguity:

     interface Ambiguous {
         public enum A implements Ambiguous { INSTANCE }
         String A = "bar"; // THIS IS A BAD IDEA.
     }
     ...
     Ambiguous.A foo = null;
     System.out.println(foo instanceof Ambiguous.A);
     String bar = "bar";
     switch(bar) {
         case Ambiguous.A -> System.out.println("A");
     }

This code is valid as of Java 14.
Ambiguous.A refers to different things, depending on whether it is used 
in an instanceof or in a switch.

I don't really know what point I want to make here. I think it stems 
from my understanding that pattern should unify instanceof and switches, 
which could have some (arguably stupid) cases where this may not work.

- Johannes



More information about the amber-dev mailing list