Why are the curly braces required for a minimal Record declaration?
John Rose
john.r.rose at oracle.com
Sun Apr 26 00:12:52 UTC 2020
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.
— John
More information about the amber-dev
mailing list