Jep 468: What about 'new' syntax?
Olexandr Rotan
rotanolexandr842 at gmail.com
Wed Nov 20 06:56:20 UTC 2024
Hello. Just a side note: AFAIK "new", by spec, guarantees that new instance
of object is returned, so it can potentially affect implementation
requirements for compilers, forcing them to copy records even if with
expression block is empty, i.e.
var b = new a with {}
Unless there is a exception for such syntax, new will mandate that b != a
(by reference), which would oblige doing basically useless extra work (
Since "with" performs shallow copy and records are immutable, so a and b
will, by any "external observer", considered the same except for identity,
if Im not missing something )
On Wed, Nov 20, 2024, 06:12 Ryan Leach <rleach at rleach.id.au> wrote:
> 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/38291337/attachment.htm>
More information about the amber-dev
mailing list