www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6207] New: Mixin template evaluated to string can convert to string mixin expression implicitly

reply d-bugmail puremagic.com writes:
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



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: -------
Jun 24 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6207




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: -------
Oct 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6207


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

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



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: -------
Oct 16 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6207


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Nov 19 2012