www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4428] New: string mixin of variable of type string fails

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4428

           Summary: string mixin of variable of type string fails
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg gmail.com> 2010-07-05
02:37:50 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4428


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |INVALID


--- Comment #1 from yebblies <yebblies gmail.com> 2010-07-05 06:07:22 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 05 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4428



--- Comment #2 from Jonathan M Davis <jmdavisProg gmail.com> 2010-07-05
18:07:00 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 05 2010