"With" for records

Brian Goetz brian.goetz at oracle.com
Sun Jun 12 16:43:33 UTC 2022



On 6/12/2022 12:21 PM, forax at univ-mlv.fr wrote:
>
>
> ------------------------------------------------------------------------
>
>     *From: *"Brian Goetz" <brian.goetz at oracle.com>
>     *To: *"Remi Forax" <forax at univ-mlv.fr>
>     *Cc: *"amber-spec-experts" <amber-spec-experts at openjdk.java.net>
>     *Sent: *Saturday, June 11, 2022 8:16:26 PM
>     *Subject: *Re: "With" for records
>
>     We also probably want a rule to _prevent_ assignment to any locals
>     *other than* the synthetic component locals.  Assigning to uplevel
>     locals from within a `with` block seems like asking for trouble;
>     the with block is like a transform on the component locals. 
>
>
> perhaps any locals other that the synthetic ones and the ones declared 
> inside the block.

Yes, sorry that's exactly what I meant.  The reason for this rule is 
perhaps not obvious, but when we get to arbitrary ctor/dtor pairs, we 
will need to do _overload selection_ based on the names used in the 
block.  And it needs to be unambiguous which assignments in the block 
are intended to be to components.  For locals declared in the block, we 
can conclude that assignments to these are not component assignments.

>
>     it is possible that the inner block might want additional
>     information from one of the enclosing `contents` variables. 
>
>
> or inside the block we may want to have access to the parameters, like in:
>   record Complex(double re, double im) {
>     Complex withRe(double re) {
>       return this with { re = re_from_outer_scope; }  // find a syntax 
> here !
>     }
>   }

My hope is we do not have to find a syntax.  As you say, we can 
introduced an intermediate local:

     int outerRe = re;
     return this with { re = outerRe }

This also is a good candidate for further refinement with our friend 
"let expressions":

     return let int outeRe = re
         in this with { re = outerRe }

>
> and two other related questions about the syntax
> - do we allow to not use curly braces if there is only one assignment
>     complex with re = 3
> - or do we allow to avoid the last semicolon if there is only one 
> assignment like in your example
>     complex with { re = 3 }

Yes, these are reasonable special cases to consider (as we do with 
single-argument lambdas, or throw on the RHS of an arrow case.)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220612/61b7aae6/attachment-0001.htm>


More information about the amber-spec-experts mailing list