If Statement with Declaration

Jerry via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 4 10:17:07 PDT 2016


On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:
> Thoughts on this sort of feature?

Love your name, Jerry ;)

Maybe something like this psuedo code:

struct Condition(T) {
     Condition(T val, bool cond) { m_val = val; m_cond = cond; }
     bool opCast(T)() if(is(T == bool)) {
         return m_cond;
     }

     alias m_val this;

private:
     T m_val;
     bool m_cond;
}

auto cond(T v, bool cond) {
     return Condition!T(v, cond);
}


Usage:

int v = 5;
if(v = cond(v, v > 4) {
     writeln("Happy coding");
}



Just notice I am not proud of that T.init.


More information about the Digitalmars-d mailing list