leibnitz27 / cfr

This is the public repository for the CFR Java decompiler
https://www.benf.org/other/cfr
MIT License
1.94k stars 249 forks source link

website - syntactic sugar - records #255

Closed Lanchon closed 3 years ago

Lanchon commented 3 years ago

from page http://www.benf.org/other/cfr/java14record_types.html:

This won't compile in 14-ea+34-1452, complaining about potentially uninitialised firstname. This might be a javac bug?

    public RecordTest9(String firstName, String lastName, RecordTest2 child) {
        if (firstName == lastName) {
            lastName = "McPointer";
        }
    }

could it be that the 'implied' argument version extends the default constructor while the explicit one replaces it?

leibnitz27 commented 3 years ago

You're absolutely right; I misread the spec for JEP359 with regards to 'may'. (or at least, I got the precedence of ', and' wrong when interpreting the binding ;) )

Thanks, tidied.

FWIW - there has been a small and annoying change between j14 and j16; you can't assign to 'this.field' inside the non-argumented canonical constructor any more!

Special consideration is provided for explicitly declaring the canonical constructor (the one whose signature matches the record's state description). The constructor may be declared without a formal parameter list (in this case, it is assumed identical to the state description), **and** any record fields which are definitely unassigned when the constructor body completes normally are implicitly initialized from their corresponding formal parameters (this.x = x) on exit.

Lanchon commented 3 years ago

you can't assign to 'this.field' inside the non-argumented canonical constructor any more!

very reasonable in my eyes. you can just assign to the local parameter for similar effect. this is the way i would have designed the language myself.

thank you!