<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Thanks Brian,</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Using a reconstruction to do the trick will be much more readable that what we have now, but what will the jvm actually do? Will it instantiate the record twice? will all defaults (maybe expensive?) be generated or optimized away?</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small">In the end large beans are a pain, records seem to be a better fit, but construction does not "feel" right. At least with withers it will improve.</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small">thanks again</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br></div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div>Roel</div><div><br></div><div>--------------------------------------------<br>Roel Meeuws<br>Email: <a href="mailto:r.j.meeuws@gmail.com" target="_blank">r.j.meeuws@gmail.com</a><br>Mob. phone: +31 (0)6 10 82 44 01<br>--------------------------------------------</div></div></div><br></div><div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br><table style="border-top:1px solid #d3d4de"><tr><td style="width:55px;padding-top:13px"><a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank"><img src="https://s-install.avcdn.net/ipm/preview/icons/icon-envelope-tick-green-avg-v1.png" alt="" width="46" height="29" style="width: 46px; height: 29px;"></a></td><td style="width:470px;padding-top:12px;color:#41424e;font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Virus-free.<a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank" style="color:#4453ea">www.avg.com</a></td></tr></table><a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"></a></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Op do 11 jan 2024 om 14:37 schreef Brian Goetz <<a href="mailto:brian.goetz@oracle.com">brian.goetz@oracle.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div>
<font size="4" face="monospace">"Can I specify parameters by name
instead of position" is a frequent request. Unfortunately there's
a lot more complexity under the waterline here than anyone wants
to talk about.<br>
<br>
The short answer to the localized question ("could we support
by-name invocation of the canonical record constructor") is "yes";
since the component names are committed as part of the classes
interface, it is relatively simple matter to support this for the
canonical constructor of records. But that begs the question:
would this be a good idea (let alone "would this actually make
people happy.") Let's talk about how this glass will seem X%
empty.<br>
<br>
First, because this is specific to records (and to a specific
constructor for records), this creates a seam between how you can
interact with records and with other classes. That means that
users have to keep a "mental dictionary" of which classes are
records and which are not. Second, because the use site is likely
to be in a separate compilation unit, you've now created an
impediment to migrating between records and their equivalent
classes, because such a migration would no longer be source
compatible (clients would fail to recompile). Third, people will
surely find this boundary to be irregular; "why can I do it with
this class and not that" or "with this constructor and not that"
or "with constructors but not with static factories" will be
frequent questions; even if these questions have good answers, it
means that we've increased the set of fiddly rules that all
developers have to internalize. And developers are rarely made
happy by "OK, you can do that on tuesday mornings only" solutions;
if anything, it creates more unhappiness that they can't do it all
week long.<br>
<br>
And that doesn't mention the elephant in the room: default
values. Most of the time, when people ask about by-name
invocation, the question of defaults for parameters they do not
name isn't far behind. And this is where it really gets messy,
because this injects new complexity into an already-complex area
of the language (overload selection), and some painful questions
about binary compatibility ("surely I can add another parameter
with a default?")<br>
<br>
However, if your record really does have reasonable defaults for
some fields (such as empty lists, not just null), the proposed
`with` mechanism would let you do what you want without a new
feature: define a "minimal constructor" with the required fields
(which fills in defaults for the others), and then combine that
with a reconstruction expression:<br>
<br>
new Foo(reqA, reqB, reqC) <br>
with { optD = 3, optQ = "Hi Bob" }<br>
<br>
and you're good. <br>
<br>
<br>
</font><br>
<div>On 1/11/2024 7:48 AM, Roel Meeuws
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Dear
all,</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">This
is my first message to this list, so I hope I am not proposing
something which has already been discussed before. But here
goes.</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">In
several projects at different companies I have seen the use of
big bean classes with lots of data inside and getters,
setters, toString, equals, hashcode added. In some projects
the boilerplate was mitigated with Lombok, but of course
recent JDKs provide the notion of Records.</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Now
records are great data carriers with much less boilerplate,
however, there is a particular problem when constructing them
for very big sets.</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Consider
a dto representing a line in some EU legal obligation report
(of which there are many) that may heve hundreds of fields.
When constructing such an object there is no idea of what
parameter is being filled in in the code that is constructing
the object</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">e.g.</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">record
IrritatinglyBigEUReportLine(</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"> long
processingId;</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
ZonedDateTime reportTime;</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
String firstName;</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
String lastName;</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
String leiCode;<br>
String legalReportingId;</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
BigDecimal riskAmount;<br>
BigDecimal mifidRiskAmount;<br>
BigDecimal mifid2FinancialRiskAmount;<br>
BigDecimal mifid2SomeOtherAmount;<br>
......</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">) {}</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">now
let's construct this:</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">var
line = new IrritatinglyBigEUReportLine(</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
12345,</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
ZonedDateTime.now(),</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
"John",</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
"Doe",</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
"529900T8BM49AURSDO55",</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
BigDecimal.valueOf("100.0"),</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
BigDecimal.valueOf("100.0"),<br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
BigDecimal.valueOf("100.0"),<br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
BigDecimal.valueOf("100.0"), // anyone knows what this value
is here?<br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"> ...</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">);</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">//
also this will give a compile error in my IDE, which parameter
is missing now?</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">Could
we introduce a better way of initialization like the
following, which is like the `with`-er syntax, I have read
Brian Goetz writing about.</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">var
line = new IrritatingLuBigEUReportLine with {</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">
<div class="gmail_default"> processingId =12345;</div>
<div class="gmail_default"> reportTime = ZonedDateTime.now();</div>
<div class="gmail_default"> firstName = "John";</div>
<div class="gmail_default"> lastName = "Doe";</div>
<div class="gmail_default"> leiCode = "529900T8BM49AURSDO55";</div>
<div class="gmail_default"> legalReportingId =
"ERE43434452ERERTTT";</div>
<div class="gmail_default"> riskAmount =
BigDecimal.valueOf("100.0");</div>
<div class="gmail_default"> mifidRiskAmount =
BigDecimal.valueOf("100.0");</div>
<div class="gmail_default"> mifid2FinancialRiskAmount =
BigDecimal.valueOf("100.0");</div>
<div class="gmail_default"> mifid2SomeOtherAmount =
BigDecimal.valueOf("100.0");</div>
<div class="gmail_default"> ...</div>
</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small">};</div>
<div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>
</div>
<div>
<div dir="ltr" class="gmail_signature">
<div>Roel</div>
<div><br>
</div>
<div>--------------------------------------------<br>
Roel Meeuws<br>
Email: <a href="mailto:r.j.meeuws@gmail.com" target="_blank">r.j.meeuws@gmail.com</a><br>
Mob. phone: +31 (0)6 10 82 44 01<br>
--------------------------------------------</div>
</div>
</div>
</div>
<div id="m_-6520745720552268387DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br>
<table style="border-top:1px solid rgb(211,212,222)">
<tbody>
<tr>
<td style="width:55px;padding-top:13px"><a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank"><img src="https://s-install.avcdn.net/ipm/preview/icons/icon-envelope-tick-green-avg-v1.png" alt="" style="width: 46px; height: 29px;" width="46" height="29"></a></td>
<td style="width:470px;padding-top:12px;color:rgb(65,66,78);font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Virus-free.<a href="http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" style="color:rgb(68,83,234)" target="_blank">www.avg.com</a></td>
</tr>
</tbody>
</table>
</div>
</blockquote>
<br>
</div>
</blockquote></div>