"Disappearing" operator methods

Robert Rat via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 29 23:31:45 PDT 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




More information about the Digitalmars-d mailing list