<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <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 class="moz-cite-prefix">On 1/11/2024 7:48 AM, Roel Meeuws
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAK-vHexjMBttNB_yZm+LCNDY6OCP2zZrN1MbYV6+g6vxuoGbmw@mail.gmail.com">
      
      <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" 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" moz-do-not-send="true" class="moz-txt-link-freetext">r.j.meeuws@gmail.com</a><br>
              Mob. phone: +31 (0)6 10 82 44 01<br>
              --------------------------------------------</div>
          </div>
        </div>
      </div>
      <div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br>
        <table style="border-top:1px solid #d3d4de">
          <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" moz-do-not-send="true"><img src="https://s-install.avcdn.net/ipm/preview/icons/icon-envelope-tick-green-avg-v1.png" alt="" style="width: 46px; height: 29px;" moz-do-not-send="true" width="46" height="29"></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" moz-do-not-send="true">www.avg.com</a></td>
            </tr>
          </tbody>
        </table>
      </div>
    </blockquote>
    <br>
  </body>
</html>