On Tuesday, 9 September 2025 at 09:59:40 UTC, Dejan Lekic wrote:

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);
    }
}
```

When commenting out the callHome() in struct C, it fails.
Obviously if callHome() is explicitly created, it works.

```
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);
    }
}
```

Console contents:
```
c:\dev\D\D_templates_tutorial\toy1\source\app.d(10): Error: no property `callHome` for `l` of type `app.CallLogger!(C)`
    l.callHome("foo", "bar");
     ^
```

Reply via email to