Optional<T> equivalent in D?

Justin Whear justin at economicmodeling.com
Fri Nov 15 15:07:48 PST 2013


On Fri, 15 Nov 2013 23:41:38 +0100, Brad Anderson wrote:

> On Friday, 15 November 2013 at 22:39:40 UTC, 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
>>
>> Is there a similar approach in D? Or maybe an equivalent is in a
>> commonly used external library?
> 
> Sounds like std.typecons.Nullable to me.

No, Nullable adds a potential null state to value types, e.g. it allows 
you to null an int.  To the best of my knowledge there is no Maybe/
Optional type in Phobos, but they're extremely easy to implement 
yourself.  Here's a Maybe type that I just threw together: http://
dpaste.dzfl.pl/e4b762ed


More information about the Digitalmars-d-learn mailing list