April 27, 2007 Re: The in operator and normal arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Myron Alexander | 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)
| |||
April 27, 2007 Re: The in operator and normal arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | 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.
| |||
April 27, 2007 Re: The in operator and normal arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Myron Alexander | "Myron Alexander" <dprogramming@myron.RMVETHISalexander.com> wrote in message news:f0tona$38c$1@digitalmars.com... > int opIn (char[] val) { > return true; > } Change it to opIn_r and it will work. (Personally I think the forward and reverse overloads should be switched, but whatever) | |||
April 27, 2007 Re: The in operator and normal arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> Change it to opIn_r and it will work. (Personally I think the forward and reverse overloads should be switched, but whatever)
>
>
Thanks Jarrett, works now.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply