multi-catch vs. multiple type patterns in a single case label

Andrew Myers andru at cs.cornell.edu
Fri Apr 8 16:24:29 UTC 2022


If you want to see what switch patterns on steroids could look like, 
check out JMatch:

https://www.cs.cornell.edu/projects/jmatch/

--Andrew

Ethan McCue wrote on 4/8/22 12:19 PM:
> The answer is that a switch does not yet support an "or" pattern. It could
> in the future, but the exact shape of switch patterns is in flux.
>
> On Fri, Apr 8, 2022, 10:05 AM Tobias Gierke <tobias.gierke at code-sourcery.de>
> wrote:
>
>> Hello,
>>
>> Having read through JEP-420, I wonder why
>>
>>       try {
>>           .... do stuff ....
>>       } catch( IOException | IllegalStateException e) {
>>           System.out.println("Something went wrong: "+e);
>>       }
>>
>> is valid but
>>
>>       Exception x = ....
>>       switch(x) {
>>            case IOException | TimeoutException e ->
>> System.out.println("Something went wrong: "+e);
>>       }
>>
>> is not. I get that something like
>>
>>       Exception x = ....
>>       switch(x) {
>>            case IOException a, IllegalStateException b ->  .....
>>       }
>>
>> would leave one of the variables undefined but why can't I bind multiple
>> type patterns to the same variable, have the compiler infer a common
>> super-type and use that to check the switch branch ?
>>
>> Regards,
>> Tobias
>>
>>



More information about the discuss mailing list