<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>From: </b>"Robbe Pincket" <robbepincket@live.be><br><b>To: </b>"amber-dev" <amber-dev@openjdk.org><br><b>Sent: </b>Monday, April 10, 2023 1:27:22 PM<br><b>Subject: </b>Named record patterns<br></blockquote></div><div><style><!--

@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}

p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style></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;">






<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi all</span></p>

<p class="MsoNormal"><span lang="EN-US">I was wondering if named record patterns `o instanceof Pair(String left, String right) pair` are still on the table for the future or not. (Not sure if 'named' record pattern is the right name?)</span></p></div></blockquote><div><br data-mce-bogus="1"></div><div>Let re-state what is the issue, currently there is no way to create a binding capturing the whole record instance, forcing people to transform the record pattern to a type pattern + when (see below).<br data-mce-bogus="1"></div><div>A previous preview was supporting this case using the syntax<br data-mce-bogus="1"></div><div>  case Pair(String left, String right) pair -> ...<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>which did not work well in case someone want the binding to be also have an annotation or be declared final<br data-mce-bogus="1"></div><div>Here is the previous syntax with "final"<br data-mce-bogus="1"></div><div>  case final Pair(String left, String right) pair -> ...<br data-mce-bogus="1"></div><div>which is not very readable, the "final" being too far apart from the binding.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>One solution is to use a keyword like "as" to add a binding at the end of a record pattern (and perhaps other patterns ?)<br data-mce-bogus="1"></div><div>  case Pair(String left, String right) as final pair -> ...</div><div>which as the advantage of grouping the binding and its keyword at the cost of adding a new keyword.<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><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;"><div class="WordSection1">

<p class="MsoNormal"><span lang="EN-US">IIRC they were shelfed due to the ambiguity that they could cause in this case, if there also exists a function `when`:</span></p>

<p class="MsoNormal"><span lang="EN-US">```</span></p>
<p class="MsoNormal"><span lang="EN-US">switch(o) {</span></p>
<p class="MsoNormal"><span lang="EN-US">    case Pair(String left, String right) when when (left == right) -> ...</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US">```</span></p>

<p class="MsoNormal"><span lang="EN-US">It feels weird to me however that this feature was shelved just for this case, when a 'simple' rule could be introduced that a record pattern can't introduce a pattern variable with the name of a contextual keyword or
 at least not `when`.</span></p>

<p class="MsoNormal"><span lang="EN-US">Are there other reasons that record patterns can't introduce a variable that matches the whole pair anymore? It feels like it could be useful, even more so with JEP 443: Unnamed Patterns and Variables (Preview)</span></p>

<p class="MsoNormal"><span lang="EN-US">```</span></p>
<p class="MsoNormal"><span lang="EN-US">if (o instanceof ColoredPoint(Point(_, int y), _) cp && y > 0) {</span></p>
<p class="MsoNormal"><span lang="EN-US">    doAction(cp);</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US">```</span></p>

<p class="MsoNormal"><span lang="EN-US">Instead of</span></p>

<p class="MsoNormal"><span lang="EN-US">```</span></p>
<p class="MsoNormal"><span lang="EN-US">if (o instanceof ColoredPoint(Point(int x, int y), Color c) && y > 0) {</span></p>
<p class="MsoNormal"><span lang="EN-US">    doAction(new ColoredPoint(new Point(x, y), c));</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US">```</span></p>

<p class="MsoNormal"><span lang="EN-US">or this, if ColoredPoint can both hold `Point` and `Point3D`</span></p>


<p class="MsoNormal"><span lang="EN-US">```</span></p>
<p class="MsoNormal"><span lang="EN-US">if (o instanceof ColoredPoint cp && cp.point() instanceof Point(_, int y) && y > 0) {</span></p>
<p class="MsoNormal"><span lang="EN-US">    doAction(cp);</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US">```</span></p>

<p class="MsoNormal"><span lang="EN-US">or if it can only hold `Point`</span></p>

<p class="MsoNormal"><span lang="EN-US">```</span></p>
<p class="MsoNormal"><span lang="EN-US">if (o instanceof ColoredPoint cp && cp.point().y() > 0) {</span></p>
<p class="MsoNormal"><span lang="EN-US">    doAction(cp);</span></p>
<p class="MsoNormal"><span lang="EN-US">}</span></p>
<p class="MsoNormal"><span lang="EN-US">```</span></p>

<p class="MsoNormal"><span lang="EN-US">Greetings</span></p>
<p class="MsoNormal"><span lang="EN-US">Robbe Pincket</span></p>
</div><br></blockquote></div></div></body></html>