Option!T

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 2 04:42:50 PDT 2014


On Wednesday, 2 July 2014 at 04:26:50 UTC, Wanderer wrote:
> Uhm, I'm sorry, but I don't see any difference between two 
> approaches - null-not null vs. one value/no value.
>
> In both cases, there is a possibility of a situation when the 
> value that "should be there", is not there. It can be either 
> because it's not available yet, or because programmer made a 
> mistake in the code. But in either case, when you try to use 
> the value that's "not there" without cheking first, you get a 
> runtime error.
>
> Correct me if I'm wrong, but "Option" being suggested here, is 
> just a nullable reference in disguise.

The difference between Option!T and allowing null references for 
T is that if you take away the possibility of T being a null 
reference, you never have to check for it, which reduces the 
number of bugs. Then you can create semantics for Option!T that 
force you to think about the possibility of lack of value.

Options also kind of fit as ranges too, so in Scala you use map 
on option types, combine several Option[T] values together into a 
Seq[T], etc.


More information about the Digitalmars-d mailing list