{"id":2668,"date":"2022-01-23T14:17:59","date_gmt":"2022-01-23T08:47:59","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2668"},"modified":"2022-01-23T14:17:59","modified_gmt":"2022-01-23T08:47:59","slug":"template-mixins-in-d-programming-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/","title":{"rendered":"Template Mixins in D Programming Language"},"content":{"rendered":"<p>When I heard the name <em><strong>Mixin<\/strong><\/em>\u00a0for the first time in the context of <em><strong>Dlang<\/strong><\/em>, I imagined it would be something similar to the <em><strong>mixins<\/strong><\/em>\u00a0of <a href=\"https:\/\/ashok-khanna.medium.com\/mixins-in-common-lisp-92bf2a6c9bff\" target=\"_blank\" rel=\"noopener\"><em><strong>Common Lisp<\/strong><\/em><\/a>, but I was completely wrong!<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p><em><strong>Mixins<\/strong><\/em> are a very interesting feature of <em><strong>D<\/strong><\/em> language. They allow code to be <em><strong>&#8220;injected&#8221;<\/strong><\/em> into the source at <em><strong>&#8220;compile-time&#8221;<\/strong><\/em> and hence facilitate <em><strong>&#8220;meta programming&#8221;<\/strong><\/em>. Although on the surface they might look like a <em><strong>&#8220;C Preprocessor&#8221;<\/strong><\/em>, they are far superior. In contrast, <em><strong>Lisp<\/strong> <strong>mixins<\/strong><\/em> are a way to combine behavior from multiple classes at runtime.<\/p>\n<p>This article is about <em><strong>&#8220;Template Mixins&#8221;,<\/strong><\/em> which are among the more widely used features of the <em><strong>D<\/strong><\/em> language.<\/p>\n<p>Let us start with a simple template mixin:<\/p>\n<figure id=\"attachment_2669\" aria-describedby=\"caption-attachment-2669\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2669\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/defn\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg\" data-orig-size=\"593,194\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642930683&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Simple Template Mixin\" data-image-description=\"&lt;p&gt;Simple Template Mixin&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Simple Template Mixin&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg\" class=\"wp-image-2669\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg?resize=500%2C164&#038;ssl=1\" alt=\"Simple Template Mixin\" width=\"500\" height=\"164\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg?resize=300%2C98&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn.jpg?w=593&amp;ssl=1 593w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2669\" class=\"wp-caption-text\"><strong>Simple Template Mixin<\/strong><\/figcaption><\/figure>\n<p>The above is a <em><strong>&#8220;parameterized&#8221;<\/strong><\/em> template mixin. It takes<span class=\"Apple-converted-space\">\u00a0<\/span><em><strong>&#8220;Type&#8221;<\/strong><\/em> as a parameter, and contains a variable definition and two function definitions, all parameterized on the <em><strong>&#8220;Type&#8221;<\/strong><\/em>.<\/p>\n<p>A mixin such as the above comes in handy when we need to write <em><strong>&#8220;similar&#8221;<\/strong><\/em> code fragments in several places. Let us suppose that we need to define a struct that defines an <em><strong>&#8220;int&#8221;<\/strong><\/em> field and a <em><strong>&#8220;getter&#8221;<\/strong><\/em> and <em><strong>&#8220;setter&#8221;<\/strong><\/em> for that field. We can use our template mixin thus:<\/p>\n<figure id=\"attachment_2670\" aria-describedby=\"caption-attachment-2670\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2670\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/example1-28\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg\" data-orig-size=\"639,374\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642930733&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using the Mixin\" data-image-description=\"&lt;p&gt;Using the Mixin&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using the Mixin&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg\" class=\"wp-image-2670\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg?resize=500%2C293&#038;ssl=1\" alt=\"Using the Mixin\" width=\"500\" height=\"293\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg?resize=300%2C176&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-1.jpg?w=639&amp;ssl=1 639w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2670\" class=\"wp-caption-text\"><strong>Using the Mixin<\/strong><\/figcaption><\/figure>\n<p>What happens is that the <em><strong>D<\/strong><\/em> compiler <em><strong>&#8220;embeds&#8221;<\/strong><\/em> the appropriately instantiated template mixin inside the struct <em><strong>&#8220;X&#8221;<\/strong><\/em>. This is shown in the block comment above.<\/p>\n<p>What if we want to define another struct, but this one requires a <em><strong>&#8220;string&#8221;<\/strong><\/em> field and the corresponding <em><strong>&#8220;getter&#8221;<\/strong><\/em> and <em><strong>&#8220;setter&#8221;<\/strong><\/em> methods? Pretty easy:<\/p>\n<figure id=\"attachment_2671\" aria-describedby=\"caption-attachment-2671\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2671\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/example2-24\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg\" data-orig-size=\"670,316\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642930761&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using the Template Mixin Again\" data-image-description=\"&lt;p&gt;Using the Template Mixin Again&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using the Template Mixin Again&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg\" class=\"wp-image-2671\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg?resize=500%2C236&#038;ssl=1\" alt=\"Using the Template Mixin Again\" width=\"500\" height=\"236\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example2-1.jpg?w=670&amp;ssl=1 670w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2671\" class=\"wp-caption-text\"><strong>Using the Template Mixin Again<\/strong><\/figcaption><\/figure>\n<p>So, how do we use these structs? As any other struct! Here is the <em><strong>&#8220;main&#8221;<\/strong><\/em> program:<\/p>\n<figure id=\"attachment_2673\" aria-describedby=\"caption-attachment-2673\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2673\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/main1-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg\" data-orig-size=\"583,401\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642935153&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Main Program\" data-image-description=\"&lt;p&gt;Main Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Main Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg\" class=\"wp-image-2673\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg?resize=500%2C344&#038;ssl=1\" alt=\"Main Program\" width=\"500\" height=\"344\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg?resize=300%2C206&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main1.jpg?w=583&amp;ssl=1 583w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2673\" class=\"wp-caption-text\"><strong>Main Program<\/strong><\/figcaption><\/figure>\n<p>When you compile and run this program, here is the output:<\/p>\n<figure id=\"attachment_2674\" aria-describedby=\"caption-attachment-2674\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2674\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/output1-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg\" data-orig-size=\"578,127\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642931142&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg\" class=\"wp-image-2674\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg?resize=500%2C110&#038;ssl=1\" alt=\"Program Output\" width=\"500\" height=\"110\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg?resize=300%2C66&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output1.jpg?w=578&amp;ssl=1 578w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2674\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>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 <em><strong>&#8220;value&#8221;<\/strong><\/em> and we cannot have the same field name more than once in a struct\/class.<\/p>\n<p>So let us try something fancier. Let us define a function that takes three arguments: <em><strong>Name<\/strong><\/em> of the mixin, <em><strong>Type<\/strong><\/em> parameter and the <em><strong>Name<\/strong><\/em> of the field. This function returns a string, which represents a well-formed mixin (but which is not a mixin yet):<\/p>\n<figure id=\"attachment_2675\" aria-describedby=\"caption-attachment-2675\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2675\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/mixin-generator\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg\" data-orig-size=\"981,279\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642945027&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Mixin Generator\" data-image-description=\"&lt;p&gt;Mixin Generator&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Mixin Generator&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg\" class=\"wp-image-2675\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg?resize=600%2C171&#038;ssl=1\" alt=\"Mixin Generator\" width=\"600\" height=\"171\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg?resize=300%2C85&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg?resize=768%2C218&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Mixin-Generator.jpg?w=981&amp;ssl=1 981w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2675\" class=\"wp-caption-text\"><strong>Mixin Generator<\/strong><\/figcaption><\/figure>\n<p>When the above function is called with three arguments, it will return a valid mixin <em><strong>&#8220;string&#8221;<\/strong> <\/em>representation. We have to then <em><strong>&#8220;make&#8221;<\/strong><\/em> it into a valid mixin:<\/p>\n<figure id=\"attachment_2676\" aria-describedby=\"caption-attachment-2676\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2676\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/mixin1-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg\" data-orig-size=\"697,342\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Converting String to Mixin\" data-image-description=\"&lt;p&gt;Converting String to Mixin&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Converting String to Mixin&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg\" class=\"wp-image-2676\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg?resize=500%2C245&#038;ssl=1\" alt=\"Converting String to Mixin\" width=\"500\" height=\"245\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg?resize=300%2C147&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin1-1.jpg?w=697&amp;ssl=1 697w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2676\" class=\"wp-caption-text\"><strong>Converting String to Mixin<\/strong><\/figcaption><\/figure>\n<p>By the way, the above is not a <em><strong>template<\/strong><\/em>\u00a0mixin, but just a <em><strong>plain<\/strong><\/em> mixin.<\/p>\n<p>We can similarly crank out another mixin with a different set of parameters:<\/p>\n<figure id=\"attachment_2677\" aria-describedby=\"caption-attachment-2677\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2677\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/mixin2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg\" data-orig-size=\"682,314\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642935760&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"String to Another Mixin\" data-image-description=\"&lt;p&gt;String to Another Mixin&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;String to Another Mixin&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg\" class=\"wp-image-2677\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg?resize=500%2C230&#038;ssl=1\" alt=\"String to Another Mixin\" width=\"500\" height=\"230\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg?resize=300%2C138&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/mixin2.jpg?w=682&amp;ssl=1 682w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2677\" class=\"wp-caption-text\"><strong>String to Another Mixin<\/strong><\/figcaption><\/figure>\n<p>Let us now define a struct that uses both the above mixins, and then write a <em><strong>&#8220;main&#8221;<\/strong><\/em> to use it:<\/p>\n<figure id=\"attachment_2678\" aria-describedby=\"caption-attachment-2678\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2678\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/main2-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg\" data-orig-size=\"741,531\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642935838&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using the Mixins\" data-image-description=\"&lt;p&gt;Using the Mixins&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using the Mixins&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg\" class=\"wp-image-2678\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg?resize=500%2C358&#038;ssl=1\" alt=\"Using the Mixins\" width=\"500\" height=\"358\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg?resize=300%2C215&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Main2.jpg?w=741&amp;ssl=1 741w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2678\" class=\"wp-caption-text\"><strong>Using the Mixins<\/strong><\/figcaption><\/figure>\n<p>And here is the output:<\/p>\n<figure id=\"attachment_2680\" aria-describedby=\"caption-attachment-2680\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2680\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/output2-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg\" data-orig-size=\"604,115\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1642946849&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg\" class=\"wp-image-2680\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg?resize=500%2C95&#038;ssl=1\" alt=\"Program Output\" width=\"500\" height=\"95\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg?resize=300%2C57&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Output2-1.jpg?w=604&amp;ssl=1 604w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2680\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The code works as expected, right? That is the power of template mixins in <strong>D<\/strong> language.<\/p>\n<p>You can download the sample code <a href=\"http:\/\/www.rangakrish.com\/downloads\/Mixins.d\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I heard the name Mixin\u00a0for the first time in the context of Dlang, I imagined it would be something similar to the mixins\u00a0of Common Lisp, but I was completely wrong!\u00a0 Mixins are a very interesting feature of D language. They allow code to be &#8220;injected&#8221; into the source at &#8220;compile-time&#8221; and hence facilitate &#8220;meta [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[302,17],"tags":[305,308,307],"class_list":["post-2668","post","type-post","status-publish","format-standard","hentry","category-d-language","category-programming","tag-dlang","tag-mixins","tag-template-mixins"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-H2","jetpack-related-posts":[{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2668,"position":0},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2658,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/09\/universal-function-call-syntax-ufcs-in-d-language\/","url_meta":{"origin":2668,"position":1},"title":"Universal Function Call Syntax (UFCS) in D Language","author":"admin","date":"January 9, 2022","format":false,"excerpt":"One of the many \"cool' features of Dlang is \"Universal Funcion Call Syntax\", which permits non-member functions to be invoked using the member function call syntax. When you add to this the fact that this applies not just to user-defined types but to primitive types as well, things get interesting.\u00a0\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Example-1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-300x234.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-300x234.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Example1-300x234.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2152,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/","url_meta":{"origin":2668,"position":2},"title":"Template-Based Text Generation","author":"admin","date":"September 28, 2020","format":false,"excerpt":"I had written earlier about natural language generation\u00a0using my iLangGen framework. I used a \"template\" text file which was instantiated dynamically based on predefined \"grammars\" and external data. The sample application I show-cased demonstrated its utility and versatility. Today I would like to touch upon a few other \"pattern\" elements\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Template File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":2668,"position":3},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2162,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/10\/11\/template-based-text-generation-part-2\/","url_meta":{"origin":2668,"position":4},"title":"Template-based Text Generation &#8211; Part 2","author":"admin","date":"October 11, 2020","format":false,"excerpt":"In my previous article, I showed how \u201ciLangGen\u201d framework facilitates text generation using templates. I talked about the various \u201cpatterns\u201d that can be used in a template. However, in that article, I did not go into the details of the \u201cEmbedded Template\u201d pattern. That is the focus of today\u2019s article.\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Main Template","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/10\/main-template-300x137.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":41,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/08\/learning-lisp-resources\/","url_meta":{"origin":2668,"position":5},"title":"Learning LISP &#8211; Resources","author":"admin","date":"October 8, 2015","format":false,"excerpt":"During my talks on Lisp Programming, I am invariably asked to suggest good books to start learning the language. Here is a (partial) list that I readily recommend. Beginner Level: ANSI Common Lisp, Paul Graham, Prentice Hall, 1996. Common LISP: A Tutorial, Wendy L.Milner, Prentice Hall, 1988. Land of LISP:\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2668"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2668\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}