Is there a simple way to check if value is null for every case?

aliak something at something.com
Mon Aug 27 19:36:29 UTC 2018


On Monday, 27 August 2018 at 14:11:32 UTC, SG wrote:
> On Monday, 27 August 2018 at 13:02:28 UTC, rikki cattermole 
> wrote:
>> So Nullable in D and C# is basically the same except C#'s has 
>> language support.
>
> The big difference is that in there I could do:
>
>    int? i = null;
>    string j = null;
>    var k = null;
>
> and test all like:
>
>     i == null;
>     j == null;
>     k == null;
>
> but in D:
>
>     Nullable!int i;
>     auto j = null;
>     string k = null;
>
>     writefln("%s", i.isNull); // I need to invoke Nullable 
> property isNull;
>     writefln("%s", i == null); // I can't just do this.
>
>     writefln("%s", j == null);
>     writefln("%s", k == null);

I hear you. IMO Nullable should be modified a bit to serve the 
purpose of turning non-nullable types in to nullable types and 
only that (right now it's confusing itself with an optional 
type). Then we could implement opEquals(typeof(null)) so that 
nullable == null would work.

I.e.

struct Nullable(T) {
   static if (isPointer!T) {
     private PointerTarget!T _value = PointerTarget!T.init;
   } else {
     private T _value = T.init;
   }
   bool opEquals(typeof(null)) {
     return isNull;
   }
}

Then Nullable!(int*) would be the same as int*. Or even better 
maybe is to give a compiler error when you try and stuff a 
nullable type inside a Nullable. Because ... why?

Cheers,
- Ali


More information about the Digitalmars-d-learn mailing list