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

aliak something at something.com
Tue Aug 28 06:33:36 UTC 2018


On Monday, 27 August 2018 at 21:48:04 UTC, Alex wrote:
> On Monday, 27 August 2018 at 19:36:29 UTC, aliak wrote:
>>
>> 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?
>>
>
> Isn't it arguable, whether this is desired? I mean, in the 
> present state you can separate the cases, whether the Nullable 
> is set or not. This is a feature.
> If Nullable!(int*) behaves like int*, it would be somewhat more 
> straight forward, which is maybe also a feature. But you loose 
> something you can do right now...

Certainly arguable :)

I'm unsure about of what you lose is worth having, is the 
problem. And I have trouble thinking of a use case?

If we implement opEquals(typeof(null)) as is now, then this code:

Nullable!(int*) n = null;
assert(n != null);

passes.

And if we have no opEquals then we can't test for native null on 
a 'null'able type O_o

Another options is:

bool opEquals(typeof(null)) {
   enum isNullInvalid = is(T == class) || is(T == interface) || 
isSomeFunction!T;
   static if (isNullInvalid)
     return isNull || data is null
   else
     return isNull;
}

I also did a quick search on google "allintext: "nullable" 
site:github.com filetype:d" and there were 0 instances of T* 
(just looked thorugh search results didn't click in).


More information about the Digitalmars-d-learn mailing list