Debug help - opDispatch - unknown member function
Dejan Lekic
dejan.lekic at gmail.com
Tue Sep 9 09:59:40 UTC 2025
On Tuesday, 9 September 2025 at 00:40:31 UTC, Brother Bill wrote:
> https://tour.dlang.org/tour/en/gems/opdispatch-opapply
>
> This states: "Any unknown member function call to that type is
> passed to opDispatch, passing the unknown member function's
> name as a string template parameter."
>
> So I tried that. But the compiler didn't like it.
> How should I play the game of passing in an unknown member
> function name?
Hopefully this slightly modified, and commented version of your
original code will
help you understand why your mixin is failing:
```d
import std.stdio;
void main() {
CallLogger!C l;
l.callA(1, 2);
l.callB("ABC");
// CallLogger does not have method called "callHome", but it
has opDispatch, so
// the compiler will lower this down to
l.opDispatch("callHome", "foo", "bar")
l.callHome("foo", "bar");
}
struct C {
void callA(int i, int j) {
}
void callB(string s) {
}
// Your mixin generates code that calls C.callHome which did
not exist
void callHome(string a, string b) {
writeln(a, "/", b);
}
}
struct CallLogger(C) {
C content;
void opDispatch(string name, T...)(T vals) {
writeln("called ", name);
// Now it works, because C now has callHome method
mixin("content." ~ name)(vals);
}
}
```
More information about the Digitalmars-d-learn
mailing list