On 02/16/2014 04:40 PM, bearophile wrote:
Timon Gehr:
assert(S(1)<S(2)); // passes. ok.
assert(S(int.min)>S(int.max)); // passes. oops.
A possible solution is to add to Phobos (unless it's already there) a
variadic templated funcion cmpBuilder() that accepts an even number of
arguments, that are seen as pairs. Usage example:
struct Foo {
int x, y;
string s;
int opCmp(in ref Foo r) {
return cmpBuilder(x, r.x, y.abs, r.y.abs, s, r.s);
}
}
Is this worth adding to Phobos?
Bye,
bearophile
IMO no (lots of repetition), but forwarding opCmp is:
struct Foo{
int x,y;
string s;
private @property order(){ return tuple(x, abs(y), s); }
mixin OrderBy!order;
}
Furthermore, the language should be updated such that the built-in types
are not special w.r.t. operators. Eg:
1.opBinary!"+"(2)
1.opCmp(2)
should work.
The following should be supported as well:
struct Foo{
int x,y;
string s;
mixin OrderBy!(()=>tuple(x, abs(y), s));
}
(Currently DMD bans function literals as members.)