<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body 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);" class="elementToProof">
<p data-line="0" class="code-line ContentPasted0" 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 data-line="3" class="code-line ContentPasted0" 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 class="ContentPasted0"> </span><em class="ContentPasted0">dead code</em><span class="ContentPasted0"> </span>(meaning that code on the RHS of a dominated case will never be executed, provably).</p>
<p data-line="6" class="code-line ContentPasted0" 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 class="ContentPasted0"> </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" class="ContentPasted0">Number</code><span class="ContentPasted0"> </span>dominates<span class="ContentPasted0"> </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" class="ContentPasted0">Integer</code>--all
values of<span class="ContentPasted0"> </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" class="ContentPasted0">Integer</code><span class="ContentPasted0"> </span>are
going to be matched by a proceeding case,<span class="ContentPasted0"> </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" class="ContentPasted0">Number</code>.
This is a compile-time error. Additionally notice that all binding variables happen to be unused.</p>
<pre data-line="10" class="code-line" 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;tab-size:4" class="ContentPasted0">switch (o) {
case Number n -> 1;
case String s -> 2;
case Integer i -> 2;
}
</code></pre>
<p data-line="16" class="code-line ContentPasted0" 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 data-line="18" class="code-line" 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;tab-size:4" class="ContentPasted0">switch (o) {
case Number _ -> 1;
case String _, Integer _-> 2;
}
</code></pre>
<p data-line="23" class="code-line ContentPasted0" 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 class="ContentPasted0"> </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" class="ContentPasted0">->
2</code>, is not dead anymore. It can be reached via<span class="ContentPasted0"> </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" class="ContentPasted0">*case
String _*, Integer _-> 2</code>. As a result, the code above is correct. It just happens that the sub-pattern<span class="ContentPasted0"> </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" class="ContentPasted0">Integer
_</code><span class="ContentPasted0"> </span>will never be reachable. This can be a warning but the overall case is correct.</p>
<p data-line="29" class="code-line ContentPasted0" 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 class="ContentPasted0"> </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" class="ContentPasted0">case</code><span class="ContentPasted0"> </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" class="ContentPasted0">Integer
_</code>). That case could be rejected (symmetrically to the first example). This seems restrictive but also a valid direction.</p>
<p data-line="36" class="code-line code-active-line ContentPasted0" 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 data-line="36" class="code-line code-active-line ContentPasted0" 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 data-line="36" class="code-line code-active-line ContentPasted0" 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 data-line="36" class="code-line code-active-line ContentPasted0" 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="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Brian Goetz <brian.goetz@oracle.com><br>
<b>Sent:</b> 26 January 2023 20:33<br>
<b>To:</b> Angelos Bimpoudis <angelos.bimpoudis@oracle.com>; amber-spec-experts <amber-spec-experts@openjdk.java.net><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 style="">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)">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)">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 class="x_inserted" style="">
<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)">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)">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 class="x_moz-cite-prefix">On 1/26/2023 5:36 AM, Angelos Bimpoudis wrote:<br>
</div>
<blockquote type="cite"><style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div class="x_elementToProof x_ContentPasted0" style="">Dear experts,</div>
<div class="x_elementToProof x_ContentPasted0" style="">
<div><br class="x_ContentPasted0">
</div>
<div class="x_ContentPasted0">The first draft of the JLS spec about unnamed patterns and variables (<a class="x_moz-txt-link-freetext" href="https://openjdk.org/jeps/8294349">https://openjdk.org/jeps/8294349</a>) is available at:</div>
<div><br class="x_ContentPasted0">
</div>
<div class="x_ContentPasted0"><a class="x_moz-txt-link-freetext" href="https://cr.openjdk.java.net/~abimpoudis/unnamed/latest/">https://cr.openjdk.java.net/~abimpoudis/unnamed/latest/</a></div>
<div><br class="x_ContentPasted0">
</div>
<div class="x_ContentPasted0">Comments very much welcomed!</div>
Angelos<br>
</div>
</blockquote>
<br>
</div>
</body>
</html>