Using template mixin, with or without mixin ?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 9 06:24:34 PDT 2017


On 04/09/2017 07:44 AM, Meta wrote:
> On Saturday, 8 April 2017 at 22:37:18 UTC, ag0aep6g wrote:
[...]
>> This line works just fine, actually. There's really no difference
>> between a normal template and a mixin template when you use it in a
>> mixin.
[...]
> Hmm, you're right, but this is not how it is supposed to behave
> according to the documentation.
>
> https://dlang.org/spec/template-mixin.html

The most fitting sentence I can find there is this: "Unlike a template 
instantiation, a template mixin's body is evaluated within the scope 
where the mixin appears, not where the template declaration is defined."

But that doesn't compare the different kinds of declarations. It 
compares the invocations: "template instantiation" vs "template mixin".

A "template instantiation" is, of course, something like `foo!()`. But 
the definition of a "template mixin" [1] may be a bit surprising. `mixin 
bar!()` is a "template mixin". It isn't and doesn't contain a "template 
instantiation". It's an independent language construct.

Also note that the spec says: "The MixinTemplateName refers to a 
TemplateDeclaration." That is, MixinTemplateName is not restricted to 
refer to a TemplateMixinDeclaration.

Actually, being pedantic, the spec doesn't say that you can use the name 
of a TemplateMixinDeclaration. Seems to be a minor oversight. PR:
https://github.com/dlang/dlang.org/pull/1627


[1] https://dlang.org/spec/template-mixin.html#TemplateMixin


More information about the Digitalmars-d-learn mailing list