Jep 468: What about 'new' syntax?
Ryan Leach
rleach at rleach.id.au
Wed Nov 20 04:11:53 UTC 2024
I've recently become aware of JEP 468, due to a post on Reddit showing off
its capability, after hearing about it in recent Java talks.
I love it, however, I think the current syntax for invoking a with {} block
inhibits readability by people, especially students of Java.
At the moment in the Java language, most instances of instance creation
(that I'm aware of) is done using the new keyword, or factory methods that
call it on your behalf.
I'd instead propose that derived record creation uses the keyword `new`
**somewhere** in the syntax, to keep with consistency.
e.g. borrowing some inspiration from java's anonymous classes, the
following syntax might work.
```
Point finalLoc = new Point with nextLoc {
x *= 2;
y *= 2;
z *= 2;
};
```
it feels a little wordy, so I'm not sure, but it might open the door to the
following future changes too, if C# style object initialization was added,
```
Point finalLoc = new Point with {
x = 2;
y = 2;
z = 2;
};
```
The thing I fear with this, is it starts looking very similar to anonymous
inner classes, if it wasn't for the with keyword and it being applied to
records, so maybe there's a good reason that I'm missing that derived
record creation looks succinctly different.
Ideally new class creation would look distinctly different from new
instance creation, but alas, it's impossible to change the past syntax.
Just as food for thought, imagining using both anonymous inner classes and
with's on classes,
```
Point finalLoc = new Point() {
// class changes
} with nextLoc {
// derived class edits
};
```
But this would likely be bad code anyway, due to mixed types being used
with 'with' blocks, and an instance being thrown away just to create the
derived class, but it's interesting to consider the syntax would be
flexible enough to work like this, at least in an abstract ideation sense.
I recognize that the JEP labelled `Analogy to pattern matching`, it shows
how the proposed syntax looks similar to a common switch expression idiom
that people have adopted for records, which is good and all, but the switch
expression uses the new keyword, making it stand out, and I wonder how
widely adopted it is vs 'new'.
I don't want to stall the feature, but I believe that using the current
syntax will impact new users in learning Java, ever so slightly.
Forgive me if I am out of pocket, I realize this looks a lot like classic
bike-shedding but I'm relatively junior in the Java sphere, and I've seen
people almost begging for newer users to provide feedback on JEPs in reddit
comments instead of talking on social media.
--
Ryan Leach
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241120/38a5d5ec/attachment.htm>
More information about the amber-dev
mailing list