Error: need this for method of type

Márcio Martins marcioapm at gmail.com
Wed Oct 23 11:40:09 UTC 2019


Hi!

Consider this simplified program:
```
import std.stdio;

struct X {
     void method(int x) {
         writeln("method called");
     }
}


void main() {
     X x;

     foreach (member; __traits(allMembers, X)) {
         alias method = __traits(getMember, x, member);
         method(1);
     }
}
```

Output:
test.d(15): Error: need this for method of type void(int x)



This one as well:
```
import std.stdio;

struct X {
     void method(int x) {
         writeln("method(int) called");
     }
}


struct Y {
     void method(ref X x) {
         foreach (member; __traits(allMembers, X)) {
             alias method = __traits(getMember, x, member);
             method(1);
         }
     }
}


void main() {
     Y y;
     X x;
     y.method(x);
}
```

Output:
```test.d(14): Error: this for method needs to be type X not type 
Y```


This is a bug, right? If not, why, and how can I get around it 
and call `method`?


More information about the Digitalmars-d-learn mailing list