Briefest summary of today's Valhalla+nullness picture

Kevin Bourrillion kevinb at google.com
Thu Jun 1 18:37:13 UTC 2023


Brian's article from yesterday walks through it all carefully. This message
is my attempt at a summary.

When I step back I'm astounded that we've come this far: most developers,
most of the time, will be able to reason like so:

"I give up things I don't need, then I let the VM do its thing, and my
performance is better. If I care to learn the gory details, I can."

(Granted, the sort of developers reading *this* thread will want more than
that, and in Brian's post they have it.)

But from *this* vantage point, here's where we're at today, as concisely as
I can manage:

1. If your class doesn't do Identity Things, make it a value class. The VM
can do smarter things now.

2. `==` isn't really an Identity Thing: it will just continue to mean
"observably identical*" as it always has. System.hashCode() remains defined
in terms of `==`. But actual identity dependence will fail (when feasible,
at compile time).

3. Any type based on that class can be marked as nullable `?` or non-null
`!`. Conceptually (if not literally), `Foo!` is a subtype of `Foo?`.

4. Nullness enforcement is "best-effort" and better than nothing. *If* you
want the gory details of what's enforced you can dig into them. There's
still room for third-party nullness analysis tools to help.

5. If a value class has a do-nothing constructor, and you're fine with
non-null variables of that type being initialized to that value, add
`implicit` to the constructor. The VM can do more smarter things now.

6. Consider adding `non-atomic` to the class; the VM can do even morer
smarter things now. The downside is that racy code (already risky) might
fail in worse ways; you decide if that worries you or not.

7. The above holds for the 8 primitive types too; yes, they are still
special, but mostly in *additional* ways, rather than exceptions to the
usual rules. `int` and `Integer!` are now nearly synonymous.

And we can finally all retire the term "primitive class" now at last. :-)

While that list is meant to have asterisks and just be "good enough for
most people most of the time", it might need some important corrections
nevertheless, so, I'm glad to hear them.


* yes float/double are weird

-- 
Kevin Bourrillion | Java/Kotlin Ecosystem Team | Google, Inc. |
kevinb at google.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-spec-experts/attachments/20230601/fe889756/attachment.htm>


More information about the valhalla-spec-experts mailing list