The in operator and normal arrays

Myron Alexander dprogramming at myron.RMVETHISalexander.com
Fri Apr 27 14:08:32 PDT 2007


Frits van Bommel wrote:
> Myron Alexander wrote:
>> I was in the process of writing the enhancement request when I had a 
>> quick peek at the documentation. The operator overloading document 
>> specifically allows for overloading the in operator with opIn and 
>> opIn_r (although doesn't say how it is supposed to be used). This got 
>> me thinking, is this an enhancement request, or a bug fix request?
> 
> You can only overload operators on structs and classes (and perhaps 
> unions). You can't overload operators purely on built-in types and arrays.
> 
> (You /could/ overload it separately for every aggregate type you 
> implement, but then it still won't work for arrays of primitive types, 
> or arrays of arrays)

Hello Frits.

I am not sure how to use the in operator. I tried to overload it in a 
class and struct but received a compiler error. This is the code I tried:

struct A {
    int opIn (char[] val) {
       return true;
    }
}

void main () {
    A a = A ();

    if ("hello" in a) {
       printf ("It's alive\n");
    }
}

The compiler error I received:
inop.d(10): Error: rvalue of in expression must be an associative array, 
not A

How do I overload the in operator?

Thanks ahead,

Myron.



More information about the Digitalmars-d mailing list