"With" for records
forax at univ-mlv.fr
forax at univ-mlv.fr
Sun Jun 12 16:21:06 UTC 2022
> 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. For example:
record Couple(int first, int second) {
Couple withMax() {
return this with {
var max = Math.max(first, second); // new local variable, it's ok
first = max;
second = max;
};
}
}
> However, we may need a story (hope not) for _accessing_ uplevel shadowed locals.
> For example:
> record R(A contents) { }
> record A(B contents) { }
> record B(int contents) { }
> R r = ...
> R rr = r with { contents = contents with { contents = 3 }}
> 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 !
}
}
we can introduce an intermediary local variable but i wonder if there is a better solution ?
record Complex(double re, double im) {
Complex withRe(double re) {
var re_from_outer_scope = re;
return this with { re = re_from_outer_scope; }
}
}
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 }
Rémi
> On 6/10/2022 11:25 AM, Brian Goetz wrote:
>>> About the declaration of local variables, in Java, there is no hiding/shadowing
>>> between local variables, so a code like this is rejected ? Or do we introduce a
>>> special rule for the hiding of implicit variables ?
>> Yes, we probably do need a special rule for this. The component names are fixed,
>> and collisions are likely, so these synthetic variables will probably have to
>> be allowed to shadow other locals.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220612/bfc1ae44/attachment.htm>
More information about the amber-spec-experts
mailing list