<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <font size="4" face="monospace">The meaning of names inside the
      block is the same as outside, with the addition of the synthetic
      component locals introduced by the `with` construct (which may
      shadow outer variables.)  So here `this` refers to the method on
      which `withReal` was invoked.<br>
      <br>
      Of course, your example fails because `this.re` is a final field
      and assigning to it is not allowed.<br>
    </font><br>
    <div class="moz-cite-prefix">On 4/30/2024 2:13 AM, Eirik Bjørsnøs
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CA+pBWhu9+02-hxCW=9-qJpRAjevg-+5ZXUNrk0S30MQ9Ng41CA@mail.gmail.com">
      
      <div dir="ltr">Hi,
        <div><br>
        </div>
        <div>JEP-468 briefly mentions that derived record expressions
          can also be used inside the record class:</div>
        <div><br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Derived
          record creation expressions can also be used inside record
          classes to simplify the implementation of basic operations:  </blockquote>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote>
        <div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">record
            Complex(double re, double im) {<br>
                Complex conjugate() { return this with { im = -im; }; }<br>
                Complex realOnly()  { return this with { im = 0; }; }<br>
                Complex imOnly()    { return this with { re = 0; }; }<br>
            }</blockquote>
          <div><br>
          </div>
          <div>I'm trying to understand what happens when a derived
            expression is used in a record method taking a parameter
            with the same name as one of the record components:</div>
          <div><br>
          </div>
          <div>record Complex(double re, double im) {<br>
                Complex withReal(double re) { return this with { re = ?;
            }; }<br>
            }<br>
          </div>
          <div><br>
          </div>
          <div>Can "this" be used inside the transformation block, like
            the following?</div>
          <div><br>
          </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">  
              Complex withReal(double re) { return this with { <a href="http://this.re" moz-do-not-send="true">this.re</a> =
            re; }; }</blockquote>
          <div><br>
          </div>
          <div>I know this particular example is a bit silly, but I
            think it may be useful to combine multiple transformations
            in a single record method, also when the method takes one or
            more parameters. People will be running into this.</div>
          <div><br>
          </div>
          <div>Perhaps this could be clarified in the JEP with an
            example or does it need clarification in the JLS? Or is it
            just me missing something, as usual? :-)</div>
          <div><br>
          </div>
          <div>Thanks,</div>
          <div>Eirik.</div>
          <div> </div>
        </div>
      </div>
    </blockquote>
    <br>
  </body>
</html>