Bug 18586 – Eponymous mixin templates

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-03-10T16:36:31Z
Last change time
2024-12-13T18:57:44Z
Assigned to
No Owner
Creator
Simen Kjaeraas
Moved to GitHub: dmd#19406 →

Comments

Comment #0 by simen.kjaras — 2018-03-10T16:36:31Z
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); }
Comment #1 by robert.schadek — 2024-12-13T18:57:44Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19406 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB