Thread overview
[Issue 6207] New: Mixin template evaluated to string can convert to string mixin expression implicitly
Jun 24, 2011
Kenji Hara
Oct 17, 2011
Kenji Hara
Oct 17, 2011
Kenji Hara
Nov 20, 2012
Walter Bright
June 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6207

           Summary: Mixin template evaluated to string can convert to
                    string mixin expression implicitly
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: k.hara.pg@gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg@gmail.com> 2011-06-24 03:48:28 PDT ---
If mixin template instantiation makes manifest constant string and it appears in expression context, the compiler converts it implicitly to string mixin expression.

This enhancement feature does not conflict with any existing features. Because normal instantiating with mixin template is illegal.

Sample code:
----
mixin template expand(string code)
{
    static if (code.length >= 2 && code[0..2] == "$x")
    {
        enum expand = `x` ~ code[2..$];
        pragma(msg, expand);
    }
    else
        enum expand = code;
}

void main()
{
    int x = 1;
    int y = expand!q{$x+2};
        // Rhs is implicitly converted to mixin(expand!(q{$x+2}))
    assert(y == 3);
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6207



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2011-10-16 18:58:34 PDT ---
We can improve the string lambda features in std.algorithm!

mixin template map(string pred)
{
    enum map = `map!((a){ return `~pred~`; })`;
}
template map(alias pred)
{
    auto map(E)(E[] r)
    {
        E[] result;
        foreach (e; r)
            result ~= pred(e);
        return result;
    }
}

void main()
{
    int b = 10;
    auto r = map!q{ a * b }([1,2,3]);
    //   --> mixin(`map!((a){ return ` ~ q{ a * b } ~ `; })`)([1,2,3])
    //   --> map!((a){ return  a * b ; })([1,2,3]);
    assert(r == [10,20,30]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6207


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2011-10-16 20:21:17 PDT ---
https://github.com/D-Programming-Language/dmd/pull/459

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6207


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2012-11-19 20:30:10 PST ---
See the pull request for more discussion.

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