Nullable with reference types

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 29 17:02:36 PDT 2015


On Monday, 29 June 2015 at 19:29:37 UTC, sigod wrote:
> Hi, everyone.
>
> ```
> import std.typecons : Nullable;
>
> class Test {}
>
> Nullable!Test test;
> assert(test.isNull);
> ```
>
> Why does `Nullable` allowed to be used with reference types 
> (e.g. classes)?
>
> P.S. I have experience with C#, where `Nullable<T>` cannot be 
> used with reference types. And it sounds logical to me.

It's a design mistake in Nullable. I would suggest that either 
never use Nullable with a type that already has a null value, or 
use the "overload" of Nullable that takes a null value, and set 
it to null. Example:

Class Test {}
alias NullableTest = Nullable!(Test, null);


More information about the Digitalmars-d-learn mailing list