Optional<T> equivalent in D?
Jonathan M Davis
jmdavisProg at gmx.com
Sat Nov 16 15:34:34 PST 2013
On Saturday, November 16, 2013 11:18:38 Meta wrote:
> On Saturday, 16 November 2013 at 05:04:42 UTC, 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 value of an Option<T> type is that it moves checking for null
> into the type system. It forces you to check for null before you
> perform any potentially NullPointerException-throwing operations,
> whereas using naked class references in Java, it's easy to
> forget, or miss a null check, or just ignore it and hope
> everything is fine. With Option<T>, you have no choice but to
> check for null before you perform an operation on the wrapped
> class reference.
If you want to use the type system to try and protect against dereferencing
null, then having wrapper which guarantees that the object _isn't_ null makes
a lot more sense, particularly when just because you used Optional<T> instead
of T mkaes no guarantees whatsoever that all of the other T's in the program
are non-null. At best, if Optional<T> is used 100% consistently, you know that
when a naked T is null, it's a bug.
> > 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.
>
> I would have to respectfully disagree with that. Proper use of an
> Option<T> type can dramatically reduce the chance of calling a
> method on a null object, or even eliminate it entirely.
Honestly, I pretty much never have problems with null pointers/references, and
my natural reaction when I see people freaking out about them is to think that
they don't know what they're doing or that they're just plain paranoid. That
doesn't mean that my natural reaction is right. It could easily be the case
that many such people are merely programming in environments different enough
from anything I've had to deal with that null is actually a real problem for
them and that it would be a real problem for me in the same situation. But in
my experience, null really isn't a problem, and it can be very useful. So,
when people freak out about it and insist on trying to get the type system to
protect them from it, it really baffles me. It feels like they're trying to take
a very useful tool out of the toolbox just because they weren't careful and
managed to scratch themselves with it once or twice.
And Java's Optional seems even more useless, because it doesn't actually
protect you against dereferencing null, and because it doesn't prevent
anything which isn't in an Optional from being null.
Much as I don't think that it's worth it, I can at least see arguments for
using NonNullable (which will end up in std.typecons eventually) to guarantee
that the object isn't null, but I really don't think that using Optional or
Nullable on a nullable type gains you anything except the illusion of
protection.
Oh, well. null seems to be a very divisive topic. There are plenty of folks
who are convinced that it's a huge disaster, and plenty of others who have no
problems with it at all and consider it to be useful. And for some reason, it
seems like the folks in Java land freak out over it a lot more than the folks
in C++ land, and aside from D, C++ is definitely the language that I've used
the most and am most comfortable with, as well as tend to agree with the
proponents of the most (though obviously, it has plenty of flaws - hence why I
prefer D).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list