Revisiting default values

Jesper Steen Møller jesper at
Wed Mar 17 18:34:54 UTC 2021

Hi list

Observation inine:

> On 17 Mar 2021, at 16.14, Brian Goetz <brian.goetz at> wrote:
> Let me propose another strategy for Bucket 3.  It could be implemented at either the VM or language level, but the latter probably needs some help from the VM anyway.  The idea is that the default value is _indistinguishable from null_.  Strawman:
>  - Classes can be marked as default-hostile (e.g., `primitive class X implements NoGoodDefault`);
>  - Prior to dereferencing a default-hostile class, a check is made against the default value, and an NPE is thrown if it is the default value;
>  - When widening to a reference type, a check is made if it is the default value, and if so, is converted to null;
>  - When narrowing from a reference type, a check is made for null, and if so, converted to the default value;
>  - It is allowable to compare `x == null`, which is intepreted as "widen x to X.ref, and compare";
> - (optional) the interface NoGoodDefault could have a method that optimizes the check, such as by using a pivot field, or the language/VM could try to automatically pick a pivot field.
> Classes which opt for NoGoodDefault will be slower than those that do not due to the check, but they will flatten. Essentially, this lets authors choose between "zero means default" and "zero means null", at some cost.
To avoid confusion, a constructor of such class should synthetically check that the finished instance is indeed “non-null”. Otherwise, an implementation may encode values so that an unsuspecting user would make a “new DubiousLocalDate(1970,1,1)” but get a value indistinguishable from null.
The compiler could ensure this in some cases, but not in the general case.

> A risk here is that ignorant users who don't understand the tradeoffs will say "oh, great, there's my nullable primitive types", overuse them, and then say "primitive types are slow, java sucks."  The goal here would be to provide _safety_ for primitive types for which the default is dangerous.


More information about the valhalla-spec-observers mailing list