void main() { string str = "writeln(\"hello world\");"; mixin(str); } fails to compile, giving the errors test.d(4): Error: argument to mixin must be a string, not (str) test.d(4): Error: argument to mixin must be a string, not (str) If you put the string in directly, void main() { mixin("writeln(\"hello world\");"); } or if you create a function which returns the string and feed that into the mixin string getStr() { return "writeln(\"hello world\");"; } void main() { mixin(getStr()); } it compiles just fine. So, for some reason, mixin expressions fail when handling string variables, but not if the string is supplied directly or as a return value from a function. Obviously, it needs to be fixed so that mixin's will actually mixin string variables rather than claiming that string variables aren't strings.
String mixins only work when the string is known at compile time. Initializing from a variable won't work because the value is not known until run time. String literals are known at compile time, and the function call is evaluated using ctfe. Maybe what you're looking for is enum? void main() { enum string str = "writeln(\"hello world\");"; mixin(str); } This is not a bug, variables cannot be used at compile time.
You're right. I didn't think that through enough. It's just so natural to split the string out into a variable when constructing it that that's what I did and was thoroughly surprised when it didn't work. It probably didn't help that I was working on a function meant to be called during CTFE rather than runtime. In any case, you're right. This is not a bug. I obviously need to get a better handle on string mixins and CTFE.