<div dir="auto">Hello!<div dir="auto"><br></div><div dir="auto">I think we should consider dead patterns as dead code, so this sample should not compile.</div><div dir="auto"><br></div><div dir="auto">With best regards,</div><div dir="auto">Tagir Valeev </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 22, 2023, 15:34 Angelos Bimpoudis <<a href="mailto:angelos.bimpoudis@oracle.com">angelos.bimpoudis@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div style="font-family:"Segoe UI","Segoe UI Web (West European)","Helvetica Neue",sans-serif;font-size:11pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Coming back to the topic of dominance for a moment before I circulate a revised draft spec.</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Dominance is the way of pattern matching to detect<span> </span><em>dead code</em><span> </span>(meaning that code on the RHS of a dominated case will never be executed, provably).</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Assume the example where<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Number</code><span> </span>dominates<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Integer</code>--all
values of<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Integer</code><span> </span>are
going to be matched by a proceeding case,<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Number</code>.
This is a compile-time error. Additionally notice that all binding variables happen to be unused.</p>
<pre dir="auto" style="margin-top:0px;padding:16px;border-radius:3px;overflow:auto;background-color:rgba(220,220,220,0.4);font-size:14px"><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">switch (o) {
case Number n -> 1;
case String s -> 2;
case Integer i -> 2;
}
</code></pre>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Under this JEP this code could be rewritten blindly into:</p>
<pre dir="auto" style="margin-top:0px;padding:16px;border-radius:3px;overflow:auto;background-color:rgba(220,220,220,0.4);font-size:14px"><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">switch (o) {
case Number _ -> 1;
case String _, Integer _-> 2;
}
</code></pre>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Under the definition of dead code above, the common case that was grouped together,<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">->
2</code>, is not dead anymore. It can be reached via<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">*case
String _*, Integer _-> 2</code>. As a result, the code above is correct. It just happens that the sub-pattern<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Integer
_</code><span> </span>will never be reachable. This can be a warning but the overall case is correct.</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
An alternative interpretation would be to treat sub-patterns as "dead code". Under that interpretation the second<span> </span><code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">case</code><span> </span>of
the second example would be dominated because there is at least one preceding sub-pattern (or whole case label with one pattern as in this case) that dominates at least one of its sub-patterns (<code style="font-family:"SF Mono",Monaco,Menlo,Consolas,"Ubuntu Mono","Liberation Mono","DejaVu Sans Mono","Courier New",monospace;font-size:1em;line-height:1.357em">Integer
_</code>). That case could be rejected (symmetrically to the first example). This seems restrictive but also a valid direction.</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
So, my question is what would be the pros and cons of each approach?</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
<br>
</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Many, thanks,</p>
<p dir="auto" style="margin-top:0px;margin-bottom:0.7em;font-family:-apple-system,"system-ui","Segoe WPC","Segoe UI",system-ui,Ubuntu,"Droid Sans",sans-serif;font-size:14px">
Aggelos</p>
<br>
</div>
<div id="m_-6873757209971852382appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_-6873757209971852382divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Brian Goetz <<a href="mailto:brian.goetz@oracle.com" target="_blank" rel="noreferrer">brian.goetz@oracle.com</a>><br>
<b>Sent:</b> 26 January 2023 20:33<br>
<b>To:</b> Angelos Bimpoudis <<a href="mailto:angelos.bimpoudis@oracle.com" target="_blank" rel="noreferrer">angelos.bimpoudis@oracle.com</a>>; amber-spec-experts <<a href="mailto:amber-spec-experts@openjdk.java.net" target="_blank" rel="noreferrer">amber-spec-experts@openjdk.java.net</a>><br>
<b>Subject:</b> Re: Draft JLS Spec about unnamed patterns and variables</font>
<div> </div>
</div>
<div><font size="4"><font face="monospace">Small wording nit... in "an unnamed declaration can be used in place of the following declarations"<br>
<br>
I'm not sure "in place of" is the right wording; I think you may just want to say "in", since the grammar permits it in all of these places. (What you're really doing here is signalling that there are places the grammar allows it, but the semantics do not;
you are going to call these out individually in the appropriate places.)<br>
<br>
Similar for the second "in place of" in this section.<br>
<br>
In 14.11.1, I might refactor the text a little further. The second sentence of the first paragraph below is about case constants only, but now comes after you talk about case patterns or case constants:<br>
<br>
</font></font>
<blockquote type="cite">
<p>A<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>label has either one or more<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constants,
or<span> </span><del style="background-color:rgb(255,224,224);text-decoration:line-through">a</del><strong style="background-color:rgb(208,255,208);font-weight:inherit;text-decoration:underline">one or more</strong><span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>pattern<strong style="background-color:rgb(208,255,208);font-weight:inherit;text-decoration:underline">s</strong>.
Every<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constant must be either (1) the<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">null</code><span> </span>literal,
(2) a constant expression (<a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.29" style="text-decoration:none;color:rgb(74,103,130)" target="_blank" rel="noreferrer">15.29</a>), or (3) the name of an enum constant (<a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.9.1" style="text-decoration:none;color:rgb(74,103,130)" target="_blank" rel="noreferrer">8.9.1</a>);
otherwise a compile-time error occurs. A<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>label that has a<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">null</code><span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constant
may have an optional<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">default</code>.</p>
<div>
<p style="padding:0pt;margin:0pt 0em 1ex">It is a compile-time error if for any<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>label with
more than one<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>patterns, any of its<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>patterns
declares one or more pattern variables.</p>
</div>
</blockquote>
<br>
I suggest:<br>
<br>
A<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>label has either one or more<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constants,
or<span> </span><del style="background-color:rgb(255,224,224);text-decoration:line-through">a</del><strong style="background-color:rgb(208,255,208);font-weight:inherit;text-decoration:underline">one or more</strong><span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>pattern<strong style="background-color:rgb(208,255,208);font-weight:inherit;text-decoration:underline">s</strong>.
<br>
<br>
<i>For a case label with case constants, </i>every<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constant must be either (1) the<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">null</code><span> </span>literal,
(2) a constant expression (<a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.29" style="text-decoration:none;color:rgb(74,103,130)" target="_blank" rel="noreferrer">15.29</a>), or (3) the name of an enum constant (<a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.9.1" style="text-decoration:none;color:rgb(74,103,130)" target="_blank" rel="noreferrer">8.9.1</a>);
otherwise a compile-time error occurs. A<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>label that has a<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">null</code><span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>constant
may have an optional<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">default</code>.<br>
<br>
<i>For a case label with case patterns</i>, it is a compile-time error if any of its<span> </span><code style="white-space:pre-wrap;font-family:"DejaVu Sans Mono","Bitstream Vera Sans Mono","Luxi Mono","Courier New",monospace">case</code><span> </span>patterns
declares one or more pattern variables.<br>
<br>
I am not sure about the definition of dominance here. If I have:<br>
<br>
case Integer _, String _: A;<br>
case Number _ : B;<br>
<br>
Number dominates Integer, but it doesn't dominate Integer|String. I think you mean "if at least one of pi..pn dominates *all* of the patterns ri..rm, no?
<br>
<br>
But I'm not even sure if this is the right formulation, because:<br>
<br>
sealed interface I permits A, B { }<br>
record A() implements I {}<br>
record B() implements I {}<br>
<br>
case A _, B _: ...<br>
case I i: ...<br>
<br>
The first case label dominates I. So I think you have to appeal to exhaustiveness:<br>
<br>
"A case label with case patterns p1...pm dominates another case label with case patterns q1...qm if the set of patterns { p1..pm } dominates each qi", no?<br>
<br>
You probably have to slightly refactor the second statement about "compile time error if dominance" accordingly.<br>
<br>
<br>
<br>
<br>
<div>On 1/26/2023 5:36 AM, Angelos Bimpoudis wrote:<br>
</div>
<blockquote type="cite">
<div>Dear experts,</div>
<div>
<div><br>
</div>
<div>The first draft of the JLS spec about unnamed patterns and variables (<a href="https://openjdk.org/jeps/8294349" target="_blank" rel="noreferrer">https://openjdk.org/jeps/8294349</a>) is available at:</div>
<div><br>
</div>
<div><a href="https://cr.openjdk.java.net/~abimpoudis/unnamed/latest/" target="_blank" rel="noreferrer">https://cr.openjdk.java.net/~abimpoudis/unnamed/latest/</a></div>
<div><br>
</div>
<div>Comments very much welcomed!</div>
Angelos<br>
</div>
</blockquote>
<br>
</div>
</div>
</blockquote></div>