The comparison to C# may be syntactically short, but is pretty different culturally.  The block of a C# `with` expression is restricted to _property assignments_.  There, they are aligning the `with` syntax to the "initialization by properties" that they already had.  So this makes total sense in C#, because doing it differently would be a glaring difference.  You could describe this comparison as "C# did it this way to put the last block in the wall, but in Java, this would be the first block."

In any case, we're deeply in danger of doing the thing that we're not supposed to do here, which is:

 - Gavin posts a document for review
 - A completely tangential observation hijacks the discussion

Your point is: "users may try to abuse withers to get another feature that they want", and we should be mindful of that.  Point taken, but I think this discussion has played out.


On 1/25/2024 8:13 AM, [email protected] wrote:


------------------------------------------------------------------------

    *From: *"Maurizio Cimadamore" <[email protected]>
    *To: *"Remi Forax" <[email protected]>, "Gavin Bierman"
    <[email protected]>
    *Cc: *"amber-spec-experts" <[email protected]>
    *Sent: *Thursday, January 25, 2024 1:41:57 PM
    *Subject: *Re: Draft JEP: Derived Record Creation (Preview)

    Looking from another angle, I think an important distinction
    between creation and _derived_ creation is that in the latter case
    you have some "fallback" values to use if the `with` block doesn't
    specify transforms for all of them. In the plain creation case,
    since the object did not exist before, there is nothing to fall
    back to - other than the default value of course, which might be a
    surprising/lousy choice in some cases. So perhaps the similarity
    between these two cases is more superficial than it looks.


Let's take a look to the cousins of Java that have a syntax equivalent to the derived record creation. As far as i know, we have C# and Rust / Javascript.

In the case of C#, the syntax is very similar to the one proposed for Java, but the block of code uses '',' instead of ';' (*).
  point with { x = 3,  y = 4 }
The syntax for creating and initializing an object in C# is new Point { x = 3,  y = 4 }.
As you can see the syntax is very similar.

In the case of Rust (or Javascript), the syntax uses a splat/spread operator at the end of the object initialization syntax,
  Point {
    x: 3,
    y: 4,
    ..point  // spread operator
  }

In all cases, the same syntax is used for the creation and the derived creation. As you said, the semantics is slighly different but in an obvious way, the creation an object requires all components to be initialized, the derived creation don't.

regards,
Rémi

(*) given that the transformation block is just code in Java, it makes sense to use '=' and ';', given those are just variable assignments.


    Maurizio

    On 24/01/2024 20:19, Remi Forax wrote:

        And as a general remarks, I hope there will be a following JEP about
        record instance creation that allows to use the syntax of a
        transformation block to initialize a record.


Reply via email to