a new contract for reference types

Peter Levart peter.levart at gmail.com
Sun May 12 09:33:13 UTC 2019


On 5/11/19 9:50 PM, Peter Levart wrote:
>
> So a question arises whether to allow calling instance methods on null 
> V or to runtime check for null (sentinel) value and throw NPE at call 
> site.
>
> Since null (sentinel) value of null capable V is represented by a real 
> state, it should be possible for instance methods to operate on that 
> state, but...
>
...
> ... what should be the outcome of invoking an instance method on a 
> null V? then?
>
>
> If you allow calling instance method on a null V:
>
>
> V v = null;
>
> v.m(); // OK
>
>
> Then the following becomes confusing for the user:
>
>
> V v = null;
>
> v.m(); // OK
>
> V? vInd = v;
>
> vInd.m(); // NPE or OK ?
>
>
> You might implement the 2nd call to operate on a re-constructed null 
> (sentinel) state of V in that case, but then throwing NPE or not 
> becomes a matter of whether V is null-sentinel capable or not, because 
> you can not construct null state when V does not declare that capability.
>
>
> So the only consistent behavior seems to be to always throw NPE on 
> null, which might involve runtime check for null sentinel state.
>

On the other hand, there might be another way out of this problem.

Suppose that all inline classes were null capable. Suppose that it was 
up to the inline class declaration to decide whether its null value is 
also part of the "normally constructable" values set or whether it is a 
value that can not possibly be constructed in a normal way (other than 
assigning null to a variable).

In that case every inline class would have a null state defined, calls 
to instance methods of null values would be possible and there will be 
no need for runtime checks for null values.


Regards, Peter




More information about the valhalla-spec-experts mailing list