Value types and nulls (original) (raw)

Stephen Colebourne scolebourne at joda.org
Fri May 18 21:04:57 UTC 2018


Going out on a limb here, I'd like to say that nulls are useful, and value types need to have a similar concept.

Why? Because null is both a pain and useful. It is widely used to represent an absence of something, and that concept applies just as much to value types as reference types. For example, int has the nullable java.lang.Integer as a companion that can be used when you need a nullable int. Note that I'm no great lover of null - within our codebase a null never crosses a compilation unit boundary. But we still need the concept of nullable int (and nullable LocalDate).

Does it have to be null? Why not Optional? Or a typed null? Or a box? Well maybe - I'm trying to keep on open mind on the exact way to represent "absence", but I'll call it null for the rest of this email.

Thus, I think that is absolutely necessary to support however it is spelled. What I really don't want is that the nullable version is punished by having the same negative performance/space cliff edge of int vs Integer. That would be hugely undesirable.

On 15 May 2018 at 23:53, John Rose <john.r.rose at oracle.com> wrote:

One way to look at it is that you are proposing two new kinds of concrete classes to go with our old object class.

- object class = what we have today: identity-rich, nullable, not flattened - value class = new, flattened*, not nullable, identity-free - enforced value-based class = new, nullable, not flattened, identity-free

This looks pretty appealing as a starting point to me and worth exploring. The key downside is the lack of flattening, which has the negative performance/space trade-off I object to.

As discussed before though, the likelihood is that many value types will have a spare bit pattern that could be used to mean null - LocalDate and Currency certainly do for example. Whereas other value types will not have a spare bit pattern Long128. Can this not be embraced? Can we also say that its not just migration that needs this

Value type category A:

Value type category B:

Existing published classes should only be migrated to category B. This allows the JVM to be much tougher when it finds mixed compilation issues with category A (just throw Error).

Effectively, this gives developers the choice between:

This seems like a desirable choice for the long-term, not just a migration hack.

thanks Stephen



More information about the valhalla-dev mailing list