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

SG s at g.com
Sun Aug 26 16:37:21 UTC 2018


Hi again,

The code below works for some cases but not for the Nullable!Type.

A way to fix it should be check the type and use ".isNull" for 
Nullabe!Type. But is there a simple way to test if value is null 
for every case?


import std.stdio, std.typecons, std.variant, std.conv;

bool foo(T)(T t){
     return (t is null);
}

class S{
     Nullable!int i;
}

void main(){
     string x = "a";
     writeln(x is null," - " ,x.foo);

     string y = null;
     writeln(y is null," - ", y.foo);

     auto z = null;
     writeln(z is null," - ", z.foo);


     S s = new S();
     writeln(s.i.isNull); // Ok

     //writeln(s.i is null); // Error
     //s.i.foo(2); // Error
}


More information about the Digitalmars-d-learn mailing list