template mixins for boilerplate

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 21 12:04:08 PDT 2014


On 06/21/14 18:01, Philippe Sigaud via Digitalmars-d-learn wrote:
> In what way is a template more reliable than the equivalent function?

> mixin template Function(string name) {
>     mixin("public static int " ~ name ~ "() { return other.module." ~
> name ~"; }");
> }
> 
> struct S {
>       mixin Function!"fctn1";
> }
> 
> And this double-mixin construction seems needlessly complicated to me,

It does not make much difference for this simple case, but doing it this
way allows for other declarations that do not need to be mixed in.
IOW if 'Function' contains more boilerplate, then it does not all need
to be written inside a string. Not to mention it saves two sets of parens. :)
A mixin template *is* slightly more reliable than a function, because
it won't be (ab-)usable in many context where a normal function call works.
But I think this choice is mostly an aesthetical (ie subjective) one.

artur


More information about the Digitalmars-d-learn mailing list