Optional<T> equivalent in D?

Russel Winder russel at winder.org.uk
Sat Nov 16 04:57:57 PST 2013


On Sat, 2013-11-16 at 00:04 -0500, Jonathan M Davis wrote:
[…]
> I really don't understand this. Optional<T> is one of the most useless ideas 
> that I've ever seen in Java. Just use null. It's built into the language. It 
> works just fine. And wrapping it in a class isn't going to make it go away. 
> Just learn to deal with null properly. I've known many programmers who have no 
> problems with null whatsoever, and I'd be worried about any programmer who is 
> so scared of it that they feel the need to wrap nullable objects in another 
> type which has its own concept of null.
> 
> The only types which aren't nullable in Java are the primitive types, and if 
> you use them in generics (like Optional<T> does), then you get a class that 
> boxes the primitive type rather than using the primitive type directly, and 
> that object is of course nullable. So, you might as well just use the class 
> that boxes the primitive type directly and set its reference to null when you 
> need it to be null. And Optional<T> doesn't even protect against null, since 
> it's perfectly possible to make its contents null. So, as far as I can see, 
> Optional<T> is utterly pointless. IMHO, it's outright bad software design.

Maybe and Optional are far from pointless and returning null is anathema
if you want to use modern fluent approaches, streaming, and generally
getting things right.

Haskell has to do things this way because there is really no choice in
pure functional languages.

Scala, Ceylon, Kotlin do things analogously so as to integrate the OO
and functional approaches into what is rapidly becoming the programming
norm. Java 8 is the beginning of Java requiring that null never be
returned from a method call. And likely soon the demise of all primitive
types in favour of efficient boxing and unboxing of reference types
(unlike what happens at present).

"Dealing with null properly" is simply a remnant of 1994 era thinking,
we have long-ago moved past this.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



More information about the Digitalmars-d-learn mailing list