Adding the ?. null verification

Vladimir Panteleev via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 19:40:50 PDT 2014


On Wednesday, 18 June 2014 at 15:42:04 UTC, Etienne wrote:
> writeln(obj.member?.nested?.val);

You can implement something similar to this using UFCS:

auto q(C)(auto ref C c)
{
     struct Q
     {
         template opDispatch(string name)
         {
             @property auto opDispatch()
             {
                 return c ? mixin(q{c.}~name) : null;
             }
         }

         @property auto dummy() { }
     }
     return Q();
}

unittest
{
     class C { C c; }
     C c = new C;
     c.c = new C;

     assert(c.q.c !is null);
     assert(c.q.c.q.c is null);
     assert(c.q.c.q.c.q.c is null);
}


More information about the Digitalmars-d mailing list