Questions on default values
Stephen Colebourne
scolebourne at joda.org
Wed Mar 17 23:48:55 UTC 2021
On Wed, 17 Mar 2021 at 15:15, Brian Goetz <brian.goetz at oracle.com> 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.
Three linked questions:
My reading of the above is that a brand new NoGoodDefault (nullable)
primitive type `YearWeek(int,int)` would work as follows:
YearWeek yw = new YearWeek(2021, 6);
YearWeek ywn = null;
YearWeek ywd = YearWeek.default;
- this code compiles
- all three variables are of the same primitive type - `YearWeek`
- all three variables are primitives, not references
- `ywn` and `ywd` are == and indistinguishable
- if these are instance variables then all three are flattened
- any method call on `ywn` or `ywd` throws NPE
- there is no such thing as `YearWeek.val` (so a developer cannot
refer to a non-null YearWeek)
Object obj = yw;
YearWeek[] arr = new YearWeek[5];
- variable 'obj' is a reference of type `YearWeek.ref`
- variable 'arr' is an array of the primitive type `YearWeek`
- `arr[0] == YearWeek.default` is true
- `arr[0] == null` is true
- the elements of the array are flattened into a contiguous piece of memory
Is this analysis correct?
(If so, then I'm pleased. I wrote some "requirements" in July 2020
exactly along these lines but never sent them to the list)
Is the performance of method calls on NoGoodDefault primitives likely
to be of the same order of magnitude as calls on references? ie. does
the extra null/default checks on a NoGoodDefault primitive type
effectively equate to those already done on reference types today?
If the analysis is correct is it now the case that there is no need
for the "reference-favoring primitive classes" concept. ie. that
`java.time.LocalDate` can be migrated to a normal NoGoodDefault fully
flattened primitive type?
(I sincerely hope so...)
thanks
Stephen
More information about the valhalla-dev
mailing list