Template Mixins in D Programming Language

Written by on January 23, 2022 in D Language, Programming with 0 Comments

When I heard the name Mixin for the first time in the context of Dlang, I imagined it would be something similar to the mixins of Common Lisp, but I was completely wrong! 

Mixins are a very interesting feature of D language. They allow code to be “injected” into the source at “compile-time” and hence facilitate “meta programming”. Although on the surface they might look like a “C Preprocessor”, they are far superior. In contrast, Lisp mixins are a way to combine behavior from multiple classes at runtime.

This article is about “Template Mixins”, which are among the more widely used features of the D language.

Let us start with a simple template mixin:

Simple Template Mixin

Simple Template Mixin

The above is a “parameterized” template mixin. It takes “Type” as a parameter, and contains a variable definition and two function definitions, all parameterized on the “Type”.

A mixin such as the above comes in handy when we need to write “similar” code fragments in several places. Let us suppose that we need to define a struct that defines an “int” field and a “getter” and “setter” for that field. We can use our template mixin thus:

Using the Mixin

Using the Mixin

What happens is that the D compiler “embeds” the appropriately instantiated template mixin inside the struct “X”. This is shown in the block comment above.

What if we want to define another struct, but this one requires a “string” field and the corresponding “getter” and “setter” methods? Pretty easy:

Using the Template Mixin Again

Using the Template Mixin Again

So, how do we use these structs? As any other struct! Here is the “main” program:

Main Program

Main Program

When you compile and run this program, here is the output:

Program Output

Program Output

Fine, but there is one limitation due to the way we have defined the template mixin. We cannot use the mixin more than once in the same struct, even if the type parameters are different. Why? It is because the name of the synthesized field is hardcoded as “value” and we cannot have the same field name more than once in a struct/class.

So let us try something fancier. Let us define a function that takes three arguments: Name of the mixin, Type parameter and the Name of the field. This function returns a string, which represents a well-formed mixin (but which is not a mixin yet):

Mixin Generator

Mixin Generator

When the above function is called with three arguments, it will return a valid mixin “string” representation. We have to then “make” it into a valid mixin:

Converting String to Mixin

Converting String to Mixin

By the way, the above is not a template mixin, but just a plain mixin.

We can similarly crank out another mixin with a different set of parameters:

String to Another Mixin

String to Another Mixin

Let us now define a struct that uses both the above mixins, and then write a “main” to use it:

Using the Mixins

Using the Mixins

And here is the output:

Program Output

Program Output

The code works as expected, right? That is the power of template mixins in D language.

You can download the sample code here.

Have a great weekend!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top