<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>"Maurizio Cimadamore" <maurizio.cimadamore@oracle.com><br><b>To: </b>"Tomáš Bleša" <blesa@anneca.cz><br><b>Cc: </b>"jdk-dev" <jdk-dev@openjdk.org><br><b>Sent: </b>Tuesday, June 13, 2023 8:03:32 PM<br><b>Subject: </b>Re: Method Chaining (enhancing Java syntax)<br></blockquote></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;"><p><br>
    </p>
    <div class="moz-cite-prefix">On 13/06/2023 18:17, Tomáš Bleša wrote:<br>
    </div>
    <blockquote cite="mid:0F2DB659-9B60-484C-8C69-0F6867D02A92@anneca.cz">“<i class="">returns the same instance it is called on</i>”. The
      fact that it is also the same type is a useful byproduct. Please
      note that I used lowercase T to emphasize it is the instance not
      type.</blockquote>
    <p>I see what you mean, but what you are describing here is dynamic
      behavior - e.g. how a method with returning "this" should be
      evaluated. The question remains: what is the static type of "this"
      in a method declaration? The JLS and the compiler would have to
      define that, and that's why at some point you veer into self-type
      territory. Granted, you *could* define the static type of Foo::m
      where the method return "this" as Foo - e.g. a weaker self-type,
      which is derived from the receiver expression seen from the
      compiler.</p>
    <p>Your translation strategy should not, thus, use "void" as a
      return type, but use whatever T the type is assumed to be at
      compile-time. There *has* to be a type there, otherwise it is not
      clear how the compiler can type-check a call to a method returning
      "this". This is not just about bytecode, and I believe your
      emphasis on coming up with a translation scheme which minimizes
      passing receiver back and forth might be misguided (after all, we
      have an optimizing compiler that will likely take care of that
      problem - via inlining - among other things).</p>
    <p>As far as I understand, the possible benefits are:</p>
    <p>* user can omit "return this" inside the method body<br>
      * covariant override are automatically added in subclasses</p>
    <p>Note that a better way to achieve same benefits is with the
      "wither" approach proposed by Brian sometimes ago:</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md" target="_blank">https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md</a><br data-mce-bogus="1"></p>
    <p>IMHO, that proposal addresses the issue of dealing with immutable
      data at a much deeper level. </p></blockquote><div><br></div><div>While waiting for withers, there is a typesafe RecordBuilder class that can be used (use this code at your own risk)<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><pre>  private static final RecordBuilder.Factory FACTORY = RecordBuilder.factory(MethodHandles.lookup());

  public static void main(String[] args) {
    record Foo(int x, String name, long def) {}

    var foo = FACTORY.builder(Foo.class)
        .with(Foo::x, 3)
        .with(Foo::name, "hello")
        .build();
    System.out.println(foo);</pre></div><div><br data-mce-bogus="1"></div><div>see <a href="https://gist.github.com/forax/b9cd176b472f064ecefbae3178b748de">https://gist.github.com/forax/b9cd176b472f064ecefbae3178b748de</a></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;"><p><br>
    </p>
    <p>Maurizio<br>
    </p></blockquote>Rémi<div><br></div></div></div></body></html>