Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eponymous mixin templates #19406

Open
dlangBugzillaToGithub opened this issue Mar 10, 2018 · 0 comments
Open

Eponymous mixin templates #19406

dlangBugzillaToGithub opened this issue Mar 10, 2018 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

Simen Kjaeraas reported this on 2018-03-10T16:36:31Z

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

CC List

  • Ali Ak
  • Mike Franklin

Description

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant