Comparison operator overloading
Márcio Martins via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Dec 6 06:41:01 PST 2015
I am writing a generic numerical array struct, and I can't find a
way to do element-wise comparison operators.
What I had envisioned was something like the following, assuming
a, b, c and m are array-like, and all operations return arrays.
auto m = (a > b) * a + 15;
auto c = a.choose(a > b)^^2;
However, it seems this can't be achieved at the moment, as far as
I can tell.
The rewriting of a >= b to a.opCmp(b) >= 0 seems limiting in the
sense that it assumes scalar-like operands and semantics, which
is surprising, and goes against the awesome flexibility of the
remaining operator overloads.
Tried to look around, but according to google, the only reference
I have seen to this was someone with a similar question in 2004,
which is surprising to say the least. I suppose either I am
missing something really obvious, or I am out of luck?
More information about the Digitalmars-d-learn