Null-Restricted and Nullable Types

Jeremy Lyon jeremy.lyon at fastmail.net
Thu Nov 13 14:52:01 UTC 2025


Hi there !

_Re: Predicate<Foo?> versus Predicate<Optional<Foo>>_

I am not thinking of syntax when I compare Foo? and Optional<Foo>. I am dreaming of never having to think about null ever again. Optional is infinitely superior to variables that might or might not be null in handling the not-present case.

It might appear that Foo? and Optional<Foo> are equivalents and the difference is syntax, but I don't think that is true. I think there is a clear semantic difference between even these two apparently similar constructs. 

Null should be thought of as a case, not as a value, specifically the case of 'not present'. If you interpret null as a case, not merely a programming convenience [1], then ! means 'definitely present', Optional and ? mean 'optionally present' (although in semantically different ways) and leaving off the ! or ? in the null-marking scheme means 'it is not known if this variable is definitely present or optionally present', which is an odd statement for a programmer to be making when they write code [2].

I prefer Optional<Foo> to Foo? because elimination of null values in favour of Optional<Foo> also allows me to assume that every variable is present, no matter what. In the case:

     var myFoo = foo( ... )

with the method foo declared as 

     Optional<X> foo( ... ) { ... }

myFoo is an Optional of X, but myFoo itself is always present and I can't access X unless it is also present. *So every variable is always present*. 

This is tremendously important, and not true with the null marking approach.

Java is my favourite language for a lot of reasons and the only language I have programmed in for years and years. But is has to be said that the idea that on any line of code any reference might be null unless you know for sure otherwise, is ridiculous. It is true that Java has that in common with many other C-derived languages, but it is still ridiculous. In practice almost no-one does the null checking they should do. Every method should check for null in every parameter and null in every variable returned as a result of a method call. And no method should ever set to null or return null.

All of this can go away by eliminating nulls entirely. I think the elimination of nulls is within Java's reach now. If nulls are eliminated, no programmer will every have to check for null ever again. And when an Optional does appear, there are well defined semantics which handle the not-present case gracefully. 

Conversely if Java goes the null-marking ? and ! route, nulls don't disappear at all. They become embedded everywhere and rather than never having to deal with null I am concerned we will be dealing with null baggage all the time, even more than now. Without any benefit. I mean, if ? appears in the code it appears to me to be less like Optional and more like any variable in current Java code. 

Those are my thoughts.

Best wishes and bye for now,
Jeremy.



Note 1: When I write that null is a convenience, it appears that it is the sort of convenience that comes back to haunt you in a bad way forever once it is introduced.

Note 2: In a null-marking scheme, if ! and ? are omitted, that is itself a sematic statement about the declaration of a variable, that programmers are not making today. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-spec-observers/attachments/20251113/004c3131/attachment-0001.htm>
-------------- next part --------------
An embedded message was scrubbed...
From: ram anvesh reddy <ramanvesh at gmail.com>
Subject: Re: Null-Restricted and Nullable Types
Date: Thu, 13 Nov 2025 17:51:45 +0530
Size: 20472
URL: <https://mail.openjdk.org/pipermail/valhalla-spec-observers/attachments/20251113/004c3131/Email-0001.eml>


More information about the valhalla-spec-observers mailing list