[patterns] Match against a enum constant

Brian Goetz brian.goetz at oracle.com
Mon Oct 2 15:28:16 UTC 2017


Right.  Also, I’d guess that mixes of patterns and constants don’t yet work, as well as switching on enums (or other constants) by their supertypes:

    Object o;
    switch (o) { 
        case ENUM_CONSTANT:
    }



> On Oct 2, 2017, at 3:22 AM, Maurizio Cimadamore <maurizio.cimadamore at oracle.com> wrote:
> 
> I believe in the medium/long run, we have plans to treat enums as a special case of constant patterns. But that's not implemented yet.
> 
> In switch, you get support for enums since enums constant have always been supported in switch. In other words, what you are seeing is, I believe, a result of how things have evolved impl-wise, rather than an explicit design choice.
> 
> Maurizio
> 
> 
> On 02/10/17 11:11, Tagir Valeev wrote:
>> Hello!
>> 
>> Currently switch can match against a enum constant, e.g.:
>> 
>>   static void switches(TimeUnit t) {
>>     switch(t) {
>>     case SECONDS:System.out.println("Matches");break;
>>     }
>>   }
>> 
>> However it seems that this is no case for __matches operator:
>> 
>>   static void matches(TimeUnit t) {
>>     if(t __matches SECONDS) {
>>       System.out.println("Matches");
>>     }
>>   }
>> 
>> This cannot be compiled (even if I specify qualified
>> TimeUnit.SECONDS). Is this intended difference between switch and
>> __matches?
>> 
>> With best regards,
>> Tagir Valeev.
> 



More information about the amber-dev mailing list