Simplifying switch labels
Brian Goetz
brian.goetz at oracle.com
Thu Jun 2 20:59:38 UTC 2022
> 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";
More information about the amber-spec-experts
mailing list