Ambiguity in Patterns that could reference both a field and type

Brian Goetz brian.goetz at oracle.com
Sat Jul 25 14:02:16 UTC 2020


Nice example.  Indeed, the existing use of constant labels in switches has a pretty complex boundary.  In your example, if bar were an enum, the compiler would require an unqualified case label.   As we reduce restrictions on the switch target, these boundary issues emerge.



Sent from my iPad

> On Jul 24, 2020, at 3:37 AM, Johannes Kuhn <info at j-kuhn.de> wrote:
> 
> 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