Option!T

Shammah Chancellor via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 2 04:50:05 PDT 2014


On 2014-07-02 04:26:47 +0000, Wanderer said:

> On Tuesday, 1 July 2014 at 20:34:05 UTC, Shammah Chancellor wrote:
>> On 2014-07-01 12:45:11 +0000, w0rp said:
>> 
>>> 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)
>> 
>> I would have disagreed with you in the past, but I just read some 
>> examples from Rust and other languages with strong type systems and I'm 
>> a convert.  I'd like to see a compiler flag to turn off null assignment 
>> to reference types to enforce Option/Nullable to be used.
>> 
>> I also liked garbage collected pointers being expressed by the type 
>> system.  Clarifies when libraries are expecting to take ownership of a 
>> pointer, etc.
>> 
>> -Shammah
> 
> 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.

I don't see a huge difference either.   I didn't realize Nullable was 
already around.  The main thing is that is valuable is that 
Nullable/Option extended to reference types, and then disabling the 
usual null assignment to references.   The reason for this is that you 
can clearly discern when nulls are possible through the type system and 
when you need to check for them.   Not every function should have to be 
prepared to check for nulls -- and can express that it doesn't by not 
accepting a Nullable type.

I wonder if we could have a compiler flag to disable null references as 
an experiment.

-Shammah



More information about the Digitalmars-d mailing list