<!DOCTYPE html><html><head><title></title></head><body><dt class="v-Message-detailsTitle"><br></dt><div style="font-family:Arial;">Hi there !</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><u>Re: Predicate<Foo?> versus Predicate<Optional<Foo>></u><br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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.</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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. <br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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].</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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:</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">     var myFoo = foo( ... )</span><br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">with the method <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">foo </span>declared as </div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">     Optional<X> foo( ... ) { ... }</span></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">myFoo </span>is an Optional of X, but <span class="font" style="font-family:menlo, consolas, monospace, sans-serif;">myFoo </span>itself is always present and I can't access X unless it is also present. <i>So every variable is always present</i>. <br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">This is tremendously important, and not true with the null marking approach.</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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.<br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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. <br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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. </div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">Those are my thoughts.</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">Best wishes and bye for now,</div><div style="font-family:Arial;">Jeremy.</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;"><div style="font-family:Arial;">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.</div><div style="font-family:Arial;"><br></div><div style="font-family:Arial;">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. </div></div><div style="font-family:Arial;"><br></div></body></html>