Count your blessings!

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jun 11 05:47:07 PDT 2009


bearophile wrote:
> In my dlibs there's a lazyOr() that takes any number of arguments and returns the first one that's true (where true is defined by a standard function boolean() that returns false for 0, null, objects/structs that have a length and where such length is zero).
> 
> A reduced (and untested) version that works with two arguments only, that is (I think) the Elvis operator:
> 
> Tx lazyOr(Tx, Ty)(Tx x, lazy Ty y) {
>     static assert(CastableTypes!(Tuple!(Tx, Ty)), "lazyOr: all items must be castable to the same type.");
>     if (boolean(x))
>         return x;
>     return y();
> }

I guess this would work better:

CommonType!(Tx, Ty) lazyOr(Tx, Ty)(Tx x, lazy Ty y) { ... }


Andrei



More information about the Digitalmars-d mailing list