Question on sealed types in pattern switch

Stephen Colebourne scolebourne at joda.org
Mon Aug 17 22:55:10 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):

 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.

But what does this simple refectoring do? Does it accept or throw on
null? Does it even compile? Is it different if it is a switch
statement rather than an expression?

 var result = switch (aShape) {
   case Circle c -> ...
   case Shape s -> ...
 }

My assumption would be that it behaves the same - exhaustive and
throws on null - given it is a basic refactoring. But recent
discussions wrt totality seem to indicate that the obvious meaning
might not be what is planned....

Stephen


More information about the amber-dev mailing list