Overloading relational operators separately; thoughts?
Chris Wright via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 28 07:15:40 PDT 2016
On Wed, 28 Sep 2016 10:06:43 +0000, pineapple wrote:
> I think the cleanest possible solution would be to allow comparison
> operators to be overridden with opBinary and opBinaryRight, as well, but
> use opCmp and opEquals instead if they exist. I can't think of any way
> this would break existing code.
What happens with this?
struct MyInt {
int value;
MyInt opBinary(string op)(MyInt other) {
return MyInt(mixin("value " ~ op ~ " other.value"));
}
}
Obvious pattern for wrapping a value. Comparisons used to be unsupported,
producing an appropriate error message:
Error: need member function opCmp() for struct MyInt to compare
Now they produce a different, opaque error message, which will have to be
created, but the current equivalent is:
Error: recursive opCmp expansion
That's not a breakage, but it is an unfortunate direction for the quality
of error messages.
More information about the Digitalmars-d
mailing list