<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>De: </b>"Brian Goetz" <brian.goetz@oracle.com><br><b>À: </b>"Remi Forax" <forax@univ-mlv.fr><br><b>Cc: </b>"Guy Steele" <guy.steele@oracle.com>, "John Rose" <john.r.rose@oracle.com>, "amber-spec-experts" <amber-spec-experts@openjdk.java.net><br><b>Envoyé: </b>Mercredi 12 Août 2020 16:29:50<br><b>Objet: </b>Re: Next up for patterns: type patterns in switch<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><br>
    <blockquote cite="mid:859639325.325160.1597239930054.JavaMail.zimbra@u-pem.fr">
      <div style="font-family: arial, helvetica, sans-serif; font-size:
        12pt; color: #000000">
        <div>
          <div><br>
          </div>
          <blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><tt><font size="+1"><tt><font size="+1"><br>
                    So, who is bothered by the fact that case #3 gets
                    Box(null), and case #6 gets Bag(null)?  Anyone? 
                    (And, if not, but you are bothered by the lack of
                    totality on the true catch-alls, why not?)</font></tt></font></tt></blockquote>
          <div><br>
          </div>
          <div>I'm bothered if the pattern are not declared as total</div>
        </div>
      </div>
    </blockquote>
    <br>
    That's too bad, because there was a proposal that might work
    (`default` enables type checking for totality on switches) and now
    you want to escalate it to something that "totally" does not work
    (`default` makes _patterns_ total.)  <br>
    <br>
    <blockquote cite="mid:859639325.325160.1597239930054.JavaMail.zimbra@u-pem.fr">
      <div style="font-family: arial, helvetica, sans-serif; font-size:
        12pt; color: #000000">
        <div>
          <div> and i believe Stephen Colebourne on amber-dev is
            proposing exactly the same rules.</div></div></div></blockquote></blockquote><div><br></div><div>in fact, it's not exactly the same rules, because Stephen rules allows "case Foo foo" and "default Foo foo" in the same switch, while i think that "case Foo foo" should be flagged as an error if the pattern is total.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><blockquote cite="mid:859639325.325160.1597239930054.JavaMail.zimbra@u-pem.fr"><div style="font-family: arial, helvetica, sans-serif; font-size:
        12pt; color: #000000"><div><div><br>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
    You should read my long explanation to Stephen over there.  TL;DR:
    this is not engaging with half of what pattern matching is really
    for, and so is coming to the wrong answer.  Totality needs to be a
    property of the pattern, not the context -- otherwise, the same
    pattern in switch doesn't mean the same pattern in instanceof, and
    each construct needs its own totality hacks.  This is just another
    bad variant; worse, in fact, that the "any x" proposal.  </blockquote><div><br></div><div>I agree that totality should be a property of a pattern not of a case, like currently "default" does.</div><div>As you said, using "any" is better because it's per pattern and not per case.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Continuing with the "default" keyword even if we may come with a better keyword latter, we are back to what Guy was proposing, "case default var x" or inside a nested-pattern "case Box(default var x)",<br data-mce-bogus="1"></div><div>default being the keyword saying it's total thus allows null.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><br>
    <br>
    Having total patterns is very important; if I have:<br>
    <br>
        record Bag<T>(T t) implements Container<T> { }<br>
        record DoubleBox<T>(InnerBox<T> x) implements
    Container<T> { }<br>
        record InnerBox<T>(T x) { }<br>
    <br>
    and I do <br>
    <br>
        switch (container) {<br>
            case DoubleBox(InnerBox(var x)): ...<br>
        }<br>
    <br>
    the outer pattern is not total on Containers (doesn't match Bag),
    but the inner pattern is once we match DoubleBox.  I want to match
    all DoubleBoxes, and destructure their contents.  If I try to wedge
    totality into patterns through switch case modifiers, then (a) the
    totality is all or nothing, at all levels, and (b) what do I do when
    I want to refactor that switch into a chain of instanceof
    operations?  Then there's no way to express the pattern I want!  
    Totality must be a property of the pattern, and then we can define
    (orthogonally, please) how the patterns interact with the enclosing
    construct.</blockquote><div><br></div><div>Right, it should be</div><div>    switch (container) {<br>        case DoubleBox(InnerBox(default var x)): ...<br>    }</div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><br>
    <br>
    <br>
    I think the mistake that is dragging you down the wrong road is the
    assumption that pattern matching is always about conditionality. 
    But destructuring is just as important as conditionality.  Sometimes
    a pattern is total (on a given type), and sometimes its not, but in
    all cases it describes the same destructuring.   What was wrong with
    the "just use case var x" proposal is that it said "you can't use
    destructuring when you're total, because a null might sneak along
    for the ride", which was just mean.  <br>
    <br></blockquote><div><br></div><div>I agree destructuring is just as important as conditionality and those two things should be orthogonal.<br data-mce-bogus="1"></div><div>But i still think having a keyword to signal that a pattern (not a case) is total is better than letting people guess.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>