Thread overview
"Disappearing" operator methods
Sep 29, 2016
Tomer Filiba
Sep 29, 2016
Marc Schütz
Sep 29, 2016
Yuxuan Shui
Sep 30, 2016
Robert Rat
September 29, 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
September 29, 2016
Please have a look at this PR:
https://github.com/dlang/dmd/pull/6140

However, the error message printed with this PR isn't particularly helpful either:
Error: incompatible types for ((5) in (m2)): 'int' and 'const(MyTable)'

You might want to add a comment there, and/or open an enhancement request at: https://issues.dlang.org/
September 29, 2016
On Thursday, 29 September 2016 at 14:34:18 UTC, Marc Schütz wrote:
> Please have a look at this PR:
> https://github.com/dlang/dmd/pull/6140
>
> However, the error message printed with this PR isn't particularly helpful either:
> Error: incompatible types for ((5) in (m2)): 'int' and 'const(MyTable)'
>
> You might want to add a comment there, and/or open an enhancement request at: https://issues.dlang.org/

This is true. But currently error messages for overloaded operator is useless in general, not just for 'in'.
September 30, 2016
On Thursday, 29 September 2016 at 12:28:54 UTC, Tomer Filiba wrote:
> 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.

Indeed, please someone has to fix this !

> Instead, supposed I had a `contains` method instead,
>
> m2.contains(5);  // Error: mutable method dtest.MyTable.contains is not callable using a const object

I also think that "mutable method" means anything. If the meaning is that the method can change the object state then "muting method" would be better.

> 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