On Monday, 27 August 2018 at 07:59:17 UTC, Simen Kjærås wrote:
That's the null propagation operator (?.). What SG asked for is the null-coalescing operator (??). Of course, this can also be implemented in D (albeit with a slight more horrible syntax):

Exactly, and I know it is an example, but it doesn't work for Variant.

I was trying something like below, I need to find a way to test for all Nullable types out there, right now only works for Nullable!int.


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

void foo(T, U...)(T t, U u) if (is(T == Nullable!U) )  {
    if(t.isNull){
        writeln(u);
        return;
    }
    writeln(t);
}

void foo(T, U...)(T t, U u) if (!is(T == Nullable!U) ){
    if(t == null){
        writeln(u);
        return;
    }
    writeln(t);
}

class C {
    Nullable!int m;
}

void main(){
    Nullable!int i;
    auto j = null;
    string k = null;
    Variant l = null;
    C c = new C();

    writefln("%s", i.isNull);
    writefln("%s", j == null);
    writefln("%s", k == null);
    writefln("%s", l == null);
    writefln("%s", c.m.isNull);

    i.foo(1);
    j.foo(2);
    k.foo(3);
    l.foo(4.3);
    c.m.foo(5);
}

Reply via email to