Bug 4428 – string mixin of variable of type string fails

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-07-05T02:37:00Z
Last change time
2010-07-05T18:07:00Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2010-07-05T02:37:50Z
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.
Comment #1 by yebblies — 2010-07-05T06:07:22Z
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.
Comment #2 by issues.dlang — 2010-07-05T18:07:00Z
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.