<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><font face="monospace">The language about "fall
through between patterns" was motivated by uncertainty about the
scope of bindings. We could have been more precise (allowing
you to fall through if you don't use `s`) but that seemed to be
just inviting questionable code, so it was coarsened. <br>
<br>
With the advent of binding-less patterns, it reopened the door
here to both fall-through and case merging, but we really wanted
the case merging. So the spec could now be refined to "can't
fall out of patterns _with bindings_." <br>
<br>
If you're asking "do we want to allow fall through here", well,
while we are not big fans of fallthrough, we're also not big
fans of "go out of our way to prohibit features we don't like",
because that often leads to more complexity in the language. So
it seemed that treating these two things -- case merging and
fall-through -- equally.<br>
</font></font><br>
<div class="moz-cite-prefix">On 2/23/2023 1:26 PM, Maurizio
Cimadamore wrote:<br>
</div>
<blockquote type="cite" cite="mid:e9ea2e8b-ac1d-5182-271d-19ad38041f11@oracle.com">
<br>
<blockquote type="cite" cite="mid:SA2PR10MB46675D8D0413CDB8D7CD009582AA9@SA2PR10MB4667.namprd10.prod.outlook.com">
<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="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>
</div>
</blockquote>
<p>Actually, it seems to me that you can rewrite the above as
follows, even w/o this JEP:</p>
<p><br>
</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 n -> 1;
case String s, Integer i-> 2;
}</code></pre>
<p>If you do, the compiler complains, but not because `Integer i`
is unreacheable. But simply because we have fall-through between
patterns.</p>
<p><br>
</p>
<p>This is defined in 14.11.1:</p>
<p> </p>
<blockquote type="cite">t is a compile-time error if there is a
statement in a switch block that consists of switch-labeled
statement groups for which both of the following are true:
<ol type="1">
<li>
<p>It is labeled with a switch label that introduces a
pattern variable.</p>
</li>
<li>
<p>There is a statement preceding it in the switch block and
that statement can complete normally (<a href="https://docs.oracle.com/javase/specs/jls/se18/html/jls-14.html#jls-14.22" moz-do-not-send="true">14.22</a>).</p>
</li>
</ol>
<p>This condition is required to exclude the possibility of a
switch labeled statement being reached for which a pattern
variable declared in its switch label is in scope but without
the pattern matching having succeeded. For example, the
statement labeled by the switch label supporting the type
pattern <code>Integer i</code> could be reached from the
preceding statement group, and so the pattern variable <code>i</code>
will not be initialized:</p>
<pre><code></code></pre>
</blockquote>
Now, you could make a case that the above restriction is
unnecessary if we have unnamed pattern binding variables... and
_if_ you go down that path, yes, you do end up with an issue when
it comes to dominance.
<p><br>
</p>
<p>But do we want to change the fall-through restriction?</p>
<p><br>
</p>
<p>Cheers<br>
Maurizio<br>
</p>
<p><br>
</p>
<blockquote type="cite" cite="mid:SA2PR10MB46675D8D0413CDB8D7CD009582AA9@SA2PR10MB4667.namprd10.prod.outlook.com">
<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="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>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b>
Brian Goetz <a class="moz-txt-link-rfc2396E" href="mailto:brian.goetz@oracle.com" moz-do-not-send="true"><brian.goetz@oracle.com></a><br>
<b>Sent:</b> 26 January 2023 20:33<br>
<b>To:</b> Angelos Bimpoudis <a class="moz-txt-link-rfc2396E" href="mailto:angelos.bimpoudis@oracle.com" moz-do-not-send="true"><angelos.bimpoudis@oracle.com></a>;
amber-spec-experts <a class="moz-txt-link-rfc2396E" href="mailto:amber-spec-experts@openjdk.java.net" moz-do-not-send="true"><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 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)" moz-do-not-send="true">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)" moz-do-not-send="true">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)" moz-do-not-send="true">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)" moz-do-not-send="true">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 moz-txt-link-freetext" href="https://openjdk.org/jeps/8294349" moz-do-not-send="true">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 moz-txt-link-freetext" href="https://cr.openjdk.java.net/~abimpoudis/unnamed/latest/" moz-do-not-send="true">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>
</blockquote>
</blockquote>
<br>
</body>
</html>