implementation of record constructor auto-initialization [was Re: instance initializer]

Vicente Romero vicente.romero at oracle.com
Fri Sep 6 18:55:07 UTC 2019


nvm, as long as the assignment happens only to the argument we are fine

On 9/6/19 2:53 PM, Vicente Romero wrote:
> I think that according to what we have discussed here this example 
> included in the spec will have to be modified:
>
> |record NonNegativePoint(int i, int j) { public NonNegativePoint { if 
> ( i < 0 ) i = Math.abs(i); //should be this.i = ... if ( j < 0 ) j = 
> Math.abs(j); // should be this.j = ... } }|
> as neither `i` nor `j` are DU for all exit paths
>
> Vicente
>
> On 9/6/19 11:11 AM, Brian Goetz wrote:
>> Given than most users don’t even realize you can say `return` in a constructor, this seems a reasonable restriction.  The whole point of a compact ctor is to NOT have to specify the field initialization.
>>
>>> On Sep 6, 2019, at 10:53 AM, Maurizio Cimadamore<maurizio.cimadamore at oracle.com>  wrote:
>>>
>>>
>>> On 06/09/2019 15:45, Brian Goetz wrote:
>>>>> Actually - not sure - what do you mean by "exit path"? We clearly can't have "returns" here. And, if we "throw" then we don't care much about initializing.
>>>>>
>>>>>
>>>> Returns are exactly what I was concerned about; currently return is allowed, though weird, in ctors.
>>>>
>>>> If you want to say “no return in compact ctor”, that’s a reasonable simplification (though we’ll battle this beast again later when we want to expand some of the compact-ctor goodies to more general class constructors.)
>>>>
>>> I think I'd slightly prefer it to restrict it that way - not out of compiler complexity (as I said, a try/catch should be enough to handle it) - but out of managing user expectations.
>>>
>>> Maurizio
>>>
>



More information about the amber-dev mailing list