Option!T

Sebastian Graf via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 1 05:35:21 PDT 2014


On Tuesday, 1 July 2014 at 11:37:15 UTC, Shammah Chancellor wrote:
> On 2013-12-10 17:28:26 +0000, Andrei Alexandrescu said:
>
>> 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
>
> Did anything ever come of this?  If there's nothing in Phobos 
> yet, I can start working on it and submit a PR.   If we can get 
> this formalized and into the good usage, it seems like we would 
> not need a .? operator.
>
> -Shammah

I'd strongly vote against an Option type, because we already have
Nullable (although I would much more like the nomenclature of
Option). Nullable is trivial to convert into a range, the
function of which could simply be provided in Phobos.

Actually, like bearophile pointed out, it's not that Option is a
range, but that ranges and Option are monads, e.g. mappable and
flattenable. Being mappable and flattenable is no unique trait of
ranges, that's where the mixup comes from I think.

Of course, we could just say that "range" is D lingo for "monad",
but that will be kind of strange in the long run...


More information about the Digitalmars-d mailing list