Question on sealed types in pattern switch
Brian Goetz
brian.goetz at oracle.com
Tue Aug 18 00:44:36 UTC 2020
> When a sealed type is used in a pattern switch, AFAIK the current
> proposal is intended to make it exhaustive with nulls rejected (just
> like enums):
That was the theory. But, as it turns out, the analogy with enums is
imperfect in exactly this way. Still assessing the alternatives.
> Shape s = ... // Shape is a sealed type of Circle, Rectangle and Hexagon
> var result = switch (aShape) {
> case Circle c -> ...
> case Rectangle r -> ...
> case Hexagon h -> ...
> }
>
> Being exhaustive and rejecting null seems like eminently desirable
> behaviour. here.
Yes, and so far, no problem. None of the patterns is total, there's no
`case null`, so, like with enums, null will be rejected by the switch
with no rule changes.
> But what does this simple refectoring do?
The short answer is it is not necessarily as simple a refactoring as you
think.
As I said, still assessing the alternatives. Stay tuned.
More information about the amber-dev
mailing list