[Issue 18586] New: Eponymous mixin templates

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 10 16:36:31 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18586

          Issue ID: 18586
           Summary: Eponymous mixin templates
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: simen.kjaras at gmail.com

The eponymous template trick is very useful in other cases, but there seems to
be an oversight in that mixin templates don't benefit from it.

The usefulness of this lies in a reduced reliance on string mixins, and a
decoupling of the mixin template arguments from the name of the mixed-in code.
Consider:

struct S {
    int n;
    mixin fun!("myfunction", "return n;");
}

mixin template fun(string funName, string body) {
    mixin("auto "~funName~"() { "~body~" }");
}

unittest {
    auto s = S(3);
    assert(s.myfunction == 3);
}

That's the code you currently have to write to mix in a function with a
specific name. With eponymous templates, the function name wouldn't need to be
passed:

struct S {
    int n;
    mixin fun!"return n;" myfunction ;
}

mixin template fun(string body) {
    mixin("auto fun() { "~body~" }");
}

unittest {
    auto s = S(3);
    assert(s.myfunction == 3);
}

--


More information about the Digitalmars-d-bugs mailing list