On Sunday, 26 August 2018 at 16:39:53 UTC, rikki cattermole wrote:
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;
}

Hi Rikki,

I'm still confused, I want to create a extension for checking null for every type, so in D I'll need to handle each case with Templates?

Because in C# I just need to compare an object/variable with null,

x == null;
myObject == null;
myObject.i == null;
myStruct == null;
myStruct.j == null;

This works fine in C#.


But again, in D I'll need the check the type? Is possible to make one Template for all cases?

The code below works, but I don't think this is a right thing to do, right?


import std.stdio, std.typecons, std.variant, std.conv;
import std.traits : isPointer;

bool foo(T)(T value) if (is(T == VariantN!32LU)) {
        return value == null;
}

bool foo(T)(T value) if (is(T == Nullable!int) || isPointer!T) {
        return value.isNull;
}

bool foo(T)(T t) if (!is(T == Nullable!int) && !is(T == VariantN!32LU)){
    if (is(T == typeof(null))){
        return true;
    }
    return (t is null);
}

class S{
    Nullable!int i;
}

void main(){
    Variant v = null;
    writeln(v == null," - " ,v.foo);

    Variant v2 = "a";
    writeln(v2 == null," - " ,v2.foo);

    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," - ", s.i.foo);
}






Reply via email to