On Friday, November 15, 2013 23:39:38 Jacek Furmankiewicz wrote: > Many other languages are starting to frown on returning null > values from methods (due to NullPointerException risks, etc) and > wrapping them instead in an Optional<T> like in > > Scala: > http://blog.danielwellman.com/2008/03/using-scalas-op.html > > Google Guava for Java: (now rolled into the base JDK for Java 8): > https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained
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. > Is there a similar approach in D? Or maybe an equivalent is in a > commonly used external library? We have std.typecons.Nullable. However, it's real value is in making it possible to have value types be null. I very much hope that no one is using it like Optional<T> to set nullable types to null without actually using null. The _only_ nullable type in D that I would consider reasonable to use with Nullable would be arrays, and that's because of how null arrays aren't properly distinguishable from empty arrays, making using "is null" with them a bit iffy. - Jonathan M Davis
