www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overriding "in" operator

reply Magnus Lie Hetland <magnus hetland.org> writes:
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
Mar 04 2011
next sibling parent reply Mafi <mafi example.org> writes:
Am 04.03.2011 17:01, schrieb Magnus Lie Hetland:
 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?-)
If you try to use it in the manner of `something in classWhichDefinesThisOpBinary` then it doesn't work because operator overloading normally overloads on the left operand (ie something). Use opBinaryRight(string op)(...) if(...) to get it working. Mafi
Mar 04 2011
parent reply Magnus Lie Hetland <magnus hetland.org> writes:
On 2011-03-04 17:06:29 +0100, Mafi said:

 If you try to use it in the manner of `something in 
 classWhichDefinesThisOpBinary` then it doesn't work because operator 
 overloading normally overloads on the left operand (ie something). Use 
 opBinaryRight(string op)(...) if(...) to get it working.
Aaah. That makes sense. And works. *But*: I copied my code from Phobos :D If you search for "in", with the quotes, in std/container.d, you'll find two occurrences. The actual live use (in RedBlackTree) is opBinaryRight (which makes sense), but in the dummy class, TotalContainer, which is described as "an unimplemented container that illustrates a host of primitives that a container may define", uses just opBinary. The doc-comment says that "$(D k in container) returns true if the given key is in the container". So ... a "bug", I guess? (One that isn't really executed -- but still...) Worth reporting? Anwyay: Thanks for the clarification :) -- Magnus Lie Hetland http://hetland.org
Mar 04 2011
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Magnus Lie Hetland <magnus hetland.org> wrote:

 On 2011-03-04 17:06:29 +0100, Mafi said:

 If you try to use it in the manner of `something in  
 classWhichDefinesThisOpBinary` then it doesn't work because operator  
 overloading normally overloads on the left operand (ie something). Use  
 opBinaryRight(string op)(...) if(...) to get it working.
Aaah. That makes sense. And works. *But*: I copied my code from Phobos :D If you search for "in", with the quotes, in std/container.d, you'll find two occurrences. The actual live use (in RedBlackTree) is opBinaryRight (which makes sense), but in the dummy class, TotalContainer, which is described as "an unimplemented container that illustrates a host of primitives that a container may define", uses just opBinary. The doc-comment says that "$(D k in container) returns true if the given key is in the container". So ... a "bug", I guess? (One that isn't really executed -- but still...) Worth reporting? Anwyay: Thanks for the clarification :)
Definitely report it. -- Simen
Mar 04 2011
prev sibling parent reply spir <denis.spir gmail.com> writes:
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
Mar 04 2011
parent reply Magnus Lie Hetland <magnus hetland.org> writes:
On 2011-03-04 18:08:08 +0100, spir said:

 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.
Huh. Cool. Works like a charm. Seems cleaner like the opBinaryRight solution, really. I just didn't know of it :) -- Magnus Lie Hetland http://hetland.org
Mar 04 2011
parent =?ISO-8859-1?Q?Ali_=C7ehreli?= <acehreli yahoo.com> writes:
On 03/04/2011 10:41 AM, Magnus Lie Hetland wrote:
 On 2011-03-04 18:08:08 +0100, spir said:

 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.
Huh. Cool. Works like a charm. Seems cleaner like the opBinaryRight solution, really. I just didn't know of it :)
It can be seen on the D1 documentation: http://www.digitalmars.com/d/1.0/operatoroverloading.html That is (or "will be") deprecated in D2. Ali
Mar 04 2011