The benefit is twofold: not only does the user not have to write the stupid cases (imagine if Box had ten slots, would we want to write the 2^10 partial null cases?), but because we throw on the remainder, DA can treat the switch as covering all boxes, and be assured there are no leaks.

More on this last point.  We don't yet have pattern assignment, but totality and remainder are key to it.  Suppose we have

    Box<String> bs = ...

and we want to destructure:

    Box(String s) = bs;
    // s had better be DA here

The pattern on the LHS must be total, but can have remainder; the statement is allowed to throw on the remainder, because the remainder is, by definition, the "silly" matches.  Here, the only remainder is null; you can't destructure a null, so if you asked to destructure a Box, you probably were assuming that it was a real box.  So this assignment NPEs on null (not unlike the corresponding switch).


Reply via email to