Is there a simple way to check if value is null for every case?
rikki cattermole
rikki at cattermole.co.nz
Sun Aug 26 16:39:53 UTC 2018
On 27/08/2018 4:37 AM, SG wrote:
> 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
> }
UFCS function called isNull.
e.g.
import std.traits : isPointer;
bool isNull(T)(T value) if (is(T == class) || isPointer!T) {
return value is null;
}
More information about the Digitalmars-d-learn
mailing list