Comments on JDK-8198408 please ?

Remi Forax forax at univ-mlv.fr
Tue Feb 27 11:25:11 UTC 2018


----- Mail original -----
> De: "Stephen Colebourne" <scolebourne at joda.org>
> À: "valhalla-dev" <valhalla-dev at openjdk.java.net>
> Envoyé: Mardi 27 Février 2018 11:20:07
> Objet: Re: Comments on JDK-8198408 please ?

> On 26 February 2018 at 21:46, John Rose <john.r.rose at oracle.com> wrote:
>> Even if we support single-level field assignment (as twisted sugar for
>> withfield) we should *not* support multiple-level field assignment.
>>
>> In fact, I'm doubting (again, after several years) the wisdom of allowing
>> even the single-level x.a = 11.  It seems to lead the user model into a
>> swamp.  And also contains a paradox, that a subexpression on the LHS
>> of an assignment gets modified, in addition to the whole LHS.
> 
> I think having a.x = 11 "modify" a would be a poor choice. My
> preferred approach is something more like this:
> 
> a = a.with(x = 11);
> 
> where multiple fields can be set:
> 
> a = a.with(x = 11, y = 12);
> 

yes, i agree with Stephen from Java the language point of view a method 'with' + named parameters is the less surprising option
(and named parameters can be restricted to only 'with').
javac should emit an invokedynamic for it and the metafactory be written to use permuteArguments and a serie of foldArguments using default/withfield.

there is still the question of how to get the empty value type,
'Complex.default' perhaps ?

in that case, we may do not need to have a special factory as users can just write:

value class Complex {
  private final double re;
  private final double im;

  public static Complex Complex(double re, double im) {
    return Complex.default.with(re: re, im: im);
  }
}
 
and with a value record,

value record Complex(double re, double im) {
  /* everything else is generated by the compiler / the jdk 
  private final double re;
  private final double im;

  public static Complex Complex(double re, double im) {
    return Complex.default.with(re: re, im: im);
  }

  public double re() { return re; }
  public double im() { return im; }
  */
}


> And potentially this could be multi-level:
> 
> eg. instead of d.c.b.a.x = 11;
> 
> d = d.with(c.b.a.x = 11);

a little too much for me, breaking the encapsulation should not be that easy
d = d.with(c: c.with(b: b.with(a: a.with(x: 11))))

> 
> No doubt the solution adopted for values would be similar to that for
> records too.

yes !

> 
> Stephen


Rémi


More information about the valhalla-dev mailing list