Overriding "in" operator

Magnus Lie Hetland magnus at hetland.org
Fri Mar 4 08:01:20 PST 2011


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?-)

-- 
Magnus Lie Hetland
http://hetland.org



More information about the Digitalmars-d-learn mailing list