You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
}
The text was updated successfully, but these errors were encountered:
Simen Kjaeraas reported this on 2018-03-10T16:36:31Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=18586
CC List
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); }The text was updated successfully, but these errors were encountered: