<div dir="ltr"><div style="font-family:monospace" class="gmail_default">Hello Robert,<br><br>I am not a part of the Amber Dev Team, but I have some thoughts.<br><br>> If a sealed type S later grows its list of permitted<br>> subtypes at some point, how should the compiler handle<br>> switch blocks with S as selector?<br>> <br>> In the absence of a catchall label<br>> <br>> (i.e. “case default -> ...” or “case S s -> ...”)<br>> <br>> the compiler already correctly issues an error that<br>> coverage is incomplete.<br>> <br>> But if the block ended with a catchall label, the<br>> compiler is silent; the best that can be hoped-for is<br>> that the coder throws a defensive exception in the<br>> catchall label (and hope that it is triggered in<br>> testing).<br>> <br>> Suggestion:<br>> <br>> Compiler should *disallows* catchall statements in switch<br>> blocks that use a sealed type in the switch selector<br>> expression.  Note that this special case does not affect<br>> all the other non-sealed-type selectors (e.g. Object o),<br>> and appears (to me) to strengthen the safety-value of<br>> sealed types in this particular scenario (similar to<br>> enums).  <br>> <br>> All in Java’s spirit of “least surprise”.<br><br>Wow, great catch. This is much trickier than it seems to be at a first glance.<br><br>You want to make sure that a developer doesn't accidentally shoot themselves in the foot when putting the sealed type itself into the switch expression when the object being compared against is already a sealed type. The reason why is that the reader might be fooled into thinking that any new additions into the sealed type will trigger an exception here, when in reality, the inclusion of the sealed type itself as a case is basically synonymous with a default clause BECAUSE THE TYPE OF THE PARAMETER TO THE SWITCH IS THE SEALED TYPE ITSELF.<br><br>Narrow is a good word to use here lol. And I think this suggestion definitely supports Java's idea of least surprise. I would have fallen for this for sure.<br><br>I agree with you, this should probably throw an error. But it definitely should at least give a warning.<br><br>If your switch expression is longer than a few lines and the permitted subclasses of the sealed type resemble the sealed type in name, then this can be a very easy pothole to fall into.</div><div style="font-family:monospace" class="gmail_default"><br></div><div style="font-family:monospace" class="gmail_default">Thank you for pointing this out!</div><div style="font-family:monospace" class="gmail_default">David Alayachew<br></div></div>