RFC: Value range propagation for if-else
"Nordlöw" via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jun 22 14:01:35 PDT 2014
> static if (this.min <= r[0] &&
> r[1] <= this.max)
BTW: I believe valueRange also enables specialization of constant
arguments inside existing functions without having to move them
to template arguments of specialized functions. Showcase:
auto pow(T)(T arg, uint n)
{
enum vr = __traits(valueRange, arg);
static if (vr.min == vr.max) // if arg is constant
static if (vr.min == 0)
return 1;
else static if (vr.min == 1)
return arg;
else static if (vr.min == 2)
return arg*arg;
else static if (vr.min == 3)
return arg*arg*arg;
// etc...
// generic case
}
I guess a __trait, named something like has[Fixed|Constant]Value,
would be useful here.
I bet there are more possibilities with these traits we haven't
thought of yet.
More information about the Digitalmars-d
mailing list