Null-Restricted and Nullable Types concern

Brian Goetz brian.goetz at oracle.com
Tue Nov 19 13:14:12 UTC 2024



On Nov 19, 2024, at 6:22 AM, Anderson Vasconcelos Pires <andvasp at gmail.com<mailto:andvasp at gmail.com>> wrote:

Hi Guys!

So, we get that adding null restriction to Java will significantly worsen the fact that "nullable" is yet another bad default; this is pretty clear.


About "nullable" being a bad default. Have you thought for value classes the default  to be not nullable?

Indeed we have. In fact, this was our initial position for a number of years, until we realized (thanks Kevin!) that this was a bad idea.

Asymmetries like this (value classes are non-nullable, identity classes are nullable) adds significant cognitive load to _reading_ code; users have to carry around a mental dictionary of what classes are nullable and what are not in order to be able to reason about nullity.  And further, asymmetries like this make it far more difficult to make migrating an identity class (like Optional) to be a value class.  In fact, much of the complexity of our intermediate designs revolved around undoing the effects of this “split default” choice.

If we think in the long future to "fix" the default to be not nullable, this would be a step.

Half steps often look like steps, but looks can be deceiving.  Getting halfway there by having a remote, (seemingly, to some users) random criteria determine whether a variable is nullable or not would not feel like a step to many.


Thinking about reading code, maybe it is not a good choice to have this difference between value and identity objects because we would have to check the variable type to understand the logic.

Exactly.  (Among other reasons.)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20241119/425e8ca3/attachment-0001.htm>


More information about the valhalla-dev mailing list