Simplifying switch labels

Dan Smith daniel.smith at oracle.com
Thu Jun 2 23:20:30 UTC 2022


> On Jun 2, 2022, at 2:59 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> 
>> Oh, I guess I missed your point here, thinking that P and Q were constants.
>> 
>> Your comment implies that the two rules that restrict usage of patterns—can't fall through past one, and can't combine one (via ',') with most other labels—could be relaxed slightly in the case of patterns that have no bindings. I suppose that's formally true, though I'm not sure it's practically all that useful. (The only non-binding pattern we have right now is a zero-component record, right? And any non-binding patterns in the future could be equivalently expressed with 'when' clauses.)
> 
> Here's an example that's not so contrived:
> 
>     String kind = switch (o) {
>         case Integer _, Long _, Short _, Character _, Byte _ -> "integral";
>         case Double _, Float _ -> "floating point";
>         case Boolean _ -> "boolean";
>         default -> "something else";
>     };
> 
> Once we have a "don't care" pattern, any pattern can become binding-less.
> 
> Looking two steps ahead, we might decide it is not so much that there can be _no_ bindings, as much as are the bindings unifiable:
> 
>     case Bag(String x), Box(String x) -> "container of string";

Okay, those are helpful illustrations, thanks.

So an argument against a syntactic prohibition on things like 'Pattern, Pattern:' is that we may support those, subject to certain semantic restrictions, in the future.

I wonder how we would try to explain 'case Integer x, null, Double x'... (Does 'x' get bound to 'null'? How?)


More information about the amber-spec-experts mailing list