Overriding "in" operator

spir denis.spir at gmail.com
Fri Mar 4 09:08:08 PST 2011


On 03/04/2011 05:01 PM, Magnus Lie Hetland wrote:
> I'm writing a collection with functionality for membership checking. I thought
> it would be nice to use the "in" operator. In the docs for std.collections I
> surmise that this is the standard way to go. From the source code, I see
> there's no special opIn, but that it can be done with the more general...
>
> bool opBinary(string op)(T k) if (op == "in") {
> ...
> }
>
> Here T is, of course, a compile-time argument of the surrounding struct or class.
>
> So ... this is used in the the Phobos source in the DMD 2.052 distro (if I'm
> not mistaken), but I can't get dmd 2.052 to accept it? I keep getting the error
> message "Error: rvalue of in expression must be an associative array, not
> Foo!(uint)".
>
> I guess either that this is a recent feature -- I didn't see it mentioned in
> Andrei's book -- and that my Phobos source is too recent for my dmd ... or that
> I'm doing something wrong elsewhere in my code, preventing the operator
> overloading to take force. Suggestions/solutions?-)

Didn't even know 'in' can be defined with opBinary...
I use opIn_r ('r' for right side, since the container stand on the right of the 
expression) everywhere, and it works fine.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list