strettosis at tutanota.com

ArturG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 10 23:38:25 PDT 2016


you could also use a simple wrapped cast

Ret castTo(Ret, T)(T t) if(is(T == class))
{
     return cast(Ret) t;
}

then do

foo.stuff[0].castTo!Dong.x.writeln;

and if you want to guard the access you could try

foo.stuff[0].castTo!Dong.cc!((d){d.x = 5;});

cc is an alias for checkCall which is a template you can find here
http://forum.dlang.org/thread/ltalqpmpscdoziserqqx@forum.dlang.org,
it treats Type.init as false and ignores the call to fun.

but its not restricted to nullables only e.g.

float someF;
iota(0, someF).writeln; // normally would throw an AssertError

someF.cc!(f => iota(0, f)).writeln; // returns an empty range 
without calling iota, as float.nan is treated as false.
same as
0.0.cc!(f => iota(0, f)).writeln;


More information about the Digitalmars-d-learn mailing list