Why are the curly braces required for a minimal Record declaration?
Remi Forax
forax at univ-mlv.fr
Sun Apr 26 02:34:40 UTC 2020
----- Mail original -----
> De: "John Rose" <john.r.rose at oracle.com>
> À: "Swaranga Sarma" <sarma.swaranga at gmail.com>
> Cc: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Dimanche 26 Avril 2020 02:12:52
> Objet: Re: Why are the curly braces required for a minimal Record declaration?
> On Apr 25, 2020, at 1:39 PM, Swaranga Sarma <sarma.swaranga at gmail.com> wrote:
>>
>> Looking at a minimal Record Point declaration:
>>
>> record Point(int x, int y) {}
>>
>> Every part of this declaration makes sense to me expect the curly braces
>> '{}'. Why is that required if I do no intend do override any part of the
>> record?
>>
>> I am probably missing something; appreciate the insight.
>
> Some sort of punctuation is needed to mark the end of the record.
> Otherwise, there would be various ambiguities, such as:
>
> class C {
> { System.out.println(“this is a block”); }
> record Point(int x, int y) // proposed syntax
> { System.out.println(“this is another block”); }
> } // end of class C
>
> Well, I seem to hear somebody say, why not allow a semicolon
> there to terminate a record with an empty body?
>
> class C {
> { System.out.println(“this is a block”); }
> record Point(int x, int y); // semicolon syntax
> { System.out.println(“this is another block”); }
> } // end of class C
>
> At this point however, it is just more uniform to mandate that
> the record terminate with a standard class body, even if it’s empty.
> So, indeed, we have:
>
> class C {
> { System.out.println(“this is a block”); }
> record Point(int x, int y) {} // empty block syntax
> { System.out.println(“this is another block”); }
> } // end of class C
>
> Sorry about that extra keystroke, but it’s a better compromise
> than introducing a whole new syntax rule just to save a keystroke.
>
> The above ambiguity uses an obscure (some say misbegotten)
> syntax in class bodies. Rest assured it is not the only place where
> ambiguity can occur. Another one is if record declarations were
> to be allowed as statements (local types) in method bodies.
records are allowed in method bodies, it's very handy for unit testing [1].
And having the curly braces at the end of records make the code easier to parse, at least for me (curly braces => type declaration, semicolon => statement).
>
> — John
Rémi
[1] https://github.com/forax/valuetype-lworld/blob/master/fr.umlv.jsonapi/src/test/java/fr/umlv/jsonapi/bind/BinderTest.java#L65
More information about the amber-dev
mailing list