instance initializer

Peter Levart peter.levart at gmail.com
Wed Oct 2 13:27:38 UTC 2019


Hi,

On 9/6/19 2:54 AM, Brian Goetz wrote:
> In that case, what we would see is that on exit, neither i nor j were 
> either DA or DU, and we would issue a compiler error.  For each field, 
> there are three possibilities:
>
>  - The field is DU on all paths out of the ctor; we initialize it from 
> the corresponding parameter.
>  - The field is DA on all paths out of the ctor; we do nothing.
>  - The field is neither DU nor DA on some path out of the ctor; error.
>
> We could extend this analysis to the init block, where if its either 
> of the first two cases, we feed that information back into the ctor 
> analysis, and always error in the third case -- the real question is 
> whether its worth the bother.

Isn't similar thing already in place for final fields? They have to be 
DA. Granted this implementation would have to be tweaked because 
generated code would depend on the outcome of the analysis. Remi's 
concern that the analysis would have to work on user code + generated 
code is perhaps unfounded. The analysis would have to work on user code 
only and the outcome would then be used to generate the automatic code.

I think John is proposing a runtime workarround: user code playing with 
constructor parameters (locals) before assigning them to the fields. In 
this case all fields would be guaranteed DA at the end. But that doesn't 
account for instance initializer block(s) as they don't have the 
constructor parameters in scope.

Regards, Peter

>
>
> On 9/5/2019 8:43 PM, John Rose wrote:
>> On Sep 5, 2019, at 4:47 PM, Vicente Romero 
>> <vicente.romero at oracle.com> wrote:
>>> right I was thinking about the case:
>>>
>>> record R(int i, int j) {
>>>      public R {  // compact constructor
>>>          if (i < 0) {
>>>              this.i = -i;
>>>          } else {
>>>              this.j = j;
>>>          }
>>>      }
>>> }
>> Correct me if I’m wrong, but we can cover cases like this by 
>> assigning to the component parameters, and let the fields get written 
>> automatically:
>>
>> record R(int i, int j) {
>>      public R {  // compact constructor
>>          if (i < 0) {
>>              i = -i;  // not this.i
>>          } else {
>>              j = i+j;  // not this.j
>>          }
>>      }
>> }
>>
>



More information about the amber-spec-experts mailing list