Template Mixins - D Programming Language

submited by
Style Pass
2024-11-22 13:30:05

A TemplateMixin takes an arbitrary set of declarations from the body of a TemplateDeclaration and inserts them into the current context.

A TemplateMixin can occur in declaration lists of modules, classes, structs, unions, or as a statement. MixinTemplateName must refer to a TemplateDeclaration or TemplateMixinDeclaration. If the TemplateDeclaration requires no parameters, TemplateArguments can be omitted.

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. It is analogous to cutting and pasting the body of the template into the location of the mixin into a nested scope. It is useful for injecting parameterized ‘boilerplate’ code, as well as for creating templated nested functions, which is not always possible with template instantiations.

A TemplateMixinDeclaration is the same as a TemplateDeclaration, but can not be instantiated outside of a TemplateMixin.

Leave a Comment