"Disappearing" operator methods
Tomer Filiba via Digitalmars-d
digitalmars-d at puremagic.com
Thu Sep 29 05:28:54 PDT 2016
Consider this code
struct MyTable {
bool opBinaryRight(string op: "in")(int x) {
return true;
}
}
Now let's use it:
MyTable m1;
assert(5 in m1);
Everything works as expected. Now suppose I had a const object
object
const MyTable m2;
5 in m2; // Error: rvalue of in expression must be an
associative array, not const(MyTable)
A super-cryptic error message. First of all, "in expressions" are
not limited to associative arrays. Second, the error makes me
think I didn't implement the operator -- but I did.
Instead, supposed I had a `contains` method instead,
m2.contains(5); // Error: mutable method dtest.MyTable.contains
is not callable using a const object
Which is the real error, of course, the one I would have hoped to
get in the first place. My code was templated and got a `T
table`, so it was nearly impossible to guess that `T` was
actually `const MyTable`
I understand operators go through rewriting rules, but it's
nearly impossible to understand what's wrong with code with such
errors.
-tomer
More information about the Digitalmars-d
mailing list