Option!T

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 1 05:45:11 PDT 2014


On Tuesday, 10 December 2013 at 17:28:26 UTC, Andrei Alexandrescu 
wrote:
> I talked to a programmer who knows Scala (among others) and he 
> mentioned the usefulness of the Option type - a zero or one 
> element collection (range in D terminology). Here's an article 
> discussing it: 
> http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
>
> We have only(x) (http://dlang.org/phobos/std_range.html#.only) 
> to be a collection of exactly one value, but not a type for "a 
> value of type T or nothing at all". Should we follow Scala's 
> example and add it?
>
>
> Andrei

I am strongly in favour of an option type and the range 
behaviour. You can express things like chain(optionalFoo, 
optionalBar, optionalBaz) and get a sequece of only non-optional 
values out of it. It's one of those sublte things where you might 
ask, "What's the point?" Once you use it a lot you can express 
some nice things with it.

I'm also strongly in favour of Option/Maybe types in general, as 
a replacement for null. null is one of my pet hates, and things 
are much nicer when the lack of a value is represented in the 
type system. (T or Option!T)


More information about the Digitalmars-d mailing list