On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote:
I'm obviously doing something wrong, but don't quite understand.
```d
mixin template helper() {
mixin("writeln(12);");
}
struct Foo {
void opDispatch(string name)() {
import std.stdio;
mixin helper!();
//mixin("writeln(12);");
}
}
void main() {
Foo.init.opDispatch!"bar"();
}
```
The compiler emits these errors about the mixin
("writeln(12);"):
unexpected `(` in declarator
basic type expected, not `12`
found `12` when expecting `)`
no identifier for declarator `writeln(_error_)`
semicolon expected following function declaration
declaration expected, not `)`
Why does the commented code work but the mixin not? Thanks for
any pointers.
This is not the purpose mixin templates are meant to serve.
They're for copying declarations into scopes (and as such only
support declarations in them). Instead, I think what you want is
```d
template helper() {
const char[] helper = `writeln(12);`;
}
struct Foo {
void opDispatch(string name)() {
import std.stdio;
mixin(helper!());
}
}
void main() {
Foo.init.opDispatch!"bar"();
}
```