D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4428 - string mixin of variable of type string fails
Summary: string mixin of variable of type string fails
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-05 02:37 UTC by Jonathan M Davis
Modified: 2010-07-05 18:07 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2010-07-05 02:37:50 UTC
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 yebblies 2010-07-05 06:07:22 UTC
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 Jonathan M Davis 2010-07-05 18:07:00 UTC
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.