"Fossies" - the Fresh Open Source Software Archive

Member "dmd2/html/d/spec/template-mixin.html" (20 Nov 2020, 28016 Bytes) of package /linux/misc/dmd.2.094.2.linux.tar.xz:


The requested HTML page contains a <FORM> tag that is unusable on "Fossies" in "automatic" (rendered) mode so that page is shown as HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 <!DOCTYPE html>
    3 <html lang="en-US">
    4 <!--
    5     Copyright (c) 1999-2020 by the D Language Foundation
    6     All Rights Reserved.
    7     https://dlang.org/foundation_overview.html
    8   -->
    9 <head>
   10 <meta charset="utf-8">
   11 <meta name="keywords" content="D programming language">
   12 <meta name="description" content="D Programming Language">
   13 <title>Template Mixins - D Programming Language</title>
   14 
   15 <link rel="stylesheet" href="../css/codemirror.css">
   16 <link rel="stylesheet" href="../css/style.css">
   17 <link rel="stylesheet" href="../css/print.css" media="print">
   18 <link rel="shortcut icon" href="../favicon.ico">
   19 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0">
   20 
   21 </head>
   22 <body id='Template Mixins' class='doc'>
   23 <script type="text/javascript">document.body.className += ' have-javascript'</script>
   24 <div id="top"><div class="helper"><div class="helper expand-container">    <div class="logo"><a href=".."><img id="logo" alt="D Logo" src="../images/dlogo.svg"></a></div>
   25     <a href="../menu.html" title="Menu" class="hamburger expand-toggle"><span>Menu</span></a>
   26     
   27 <div id="cssmenu"><ul>    <li><a href='https://tour.dlang.org'><span>Learn</span></a></li>
   28     <li class='expand-container'><a class='expand-toggle' href='../documentation.html'><span>Documentation</span></a>
   29       
   30 <ul class='expand-content'>    <li><a href='../spec/spec.html'>Language Reference</a></li>
   31     <li><a href='../phobos/index.html'>Library Reference</a></li>
   32     <li><a href='../dmd.html'>Command-line Reference</a></li>
   33     <li class="menu-divider"><a href='../comparison.html'>Feature Overview</a></li>
   34     <li><a href='../articles.html'>Articles</a></li>
   35  </ul></li>
   36     <li><a href='../download.html'><span>Downloads</span></a></li>
   37     <li><a href='https://code.dlang.org'><span>Packages</span></a></li>
   38     <li class='expand-container'><a class='expand-toggle' href='../community.html'><span>Community</span></a>
   39       
   40 <ul class='expand-content'>    <li><a href='https://dlang.org/blog'>Blog</a></li>
   41     <li><a href='../orgs-using-d.html'>Orgs using D</a></li>
   42     <li><a href='https://twitter.com/search?q=%23dlang'>Twitter</a></li>
   43     <li><a href='../calendar.html'>Calendar</a></li>
   44     <li class="menu-divider"><a href='https://forum.dlang.org'>Forums</a></li>
   45     <li><a href='irc://irc.freenode.net/d'>IRC</a></li>
   46     <li><a href='https://discord.gg/bMZk9Q4'>Community Discord</a></li>
   47     <li><a href='https://wiki.dlang.org'>Wiki</a></li>
   48     <li class="menu-divider"><a href='https://github.com/dlang'>GitHub</a></li>
   49     <li><a href='../bugstats.html'>Issues</a></li>
   50     <li><a href='https://wiki.dlang.org/Get_involved'>Get involved</a></li>
   51     <li class="menu-divider"><a href='../foundation/contributors.html'>Contributors</a></li>
   52     <li><a href='../foundation/index.html'>Foundation</a></li>
   53     <li><a href='..//security.html'>Security Team</a></li>
   54     <li><a href='../foundation/donate.html'>Donate</a></li>
   55     <li><a href='../foundation/sponsors.html'>Sponsors</a></li>
   56  </ul></li>
   57     <li class='expand-container'><a class='expand-toggle' href='../resources.html'><span>Resources</span></a>
   58       
   59 <ul class='expand-content'>    <li><a href='https://tour.dlang.org'>Tour</a></li>
   60     <li><a href='https://wiki.dlang.org/Books'>Books</a></li>
   61     <li><a href='https://wiki.dlang.org/Tutorials'>Tutorials</a></li>
   62     <li class="menu-divider"><a href='https://wiki.dlang.org/Development_tools'>Tools</a></li>
   63     <li><a href='https://wiki.dlang.org/Editors'>Editors</a></li>
   64     <li><a href='https://wiki.dlang.org/IDEs'>IDEs</a></li>
   65     <li><a href='https://run.dlang.io'>run.dlang.io</a></li>
   66     <li><a href='http://rainers.github.io/visuald/visuald/StartPage.html'>Visual D</a></li>
   67     <li class="menu-divider"><a href='../acknowledgements.html'>Acknowledgments</a></li>
   68     <li><a href='../dstyle.html'>D Style</a></li>
   69     <li><a href='../glossary.html'>Glossary</a></li>
   70     <li><a href='../sitemap.html'>Sitemap</a></li>
   71  </ul></li>
   72 </ul></div>
   73     <div class="search-container expand-container">        <a href="../search.html" class="expand-toggle" title="Search"><span>Search</span></a>
   74         
   75     <div id="search-box">        <form method="get" action="https://google.com/search">
   76             <input type="hidden" id="domains" name="domains" value="dlang.org">
   77             <input type="hidden" id="sourceid" name="sourceid" value="google-search">
   78             <span id="search-query"><input id="q" name="q" placeholder="Search"></span><span id="search-dropdown"><span class="helper">                <select id="sitesearch" name="sitesearch" size="1">
   79                     <option value="dlang.org">Entire Site</option>
   80                     <option selected value="dlang.org/spec">Language</option>
   81                     <option  value="dlang.org/phobos">Library</option>
   82                     <option  value="forum.dlang.org">Forums</option>
   83                     
   84                 </select>
   85             </span></span><span id="search-submit"><button type="submit"><i class="fa fa-search"></i><span>go</span></button></span>
   86         </form>
   87     </div>
   88     </div>
   89 </div></div></div>
   90 
   91 <div class="container">    
   92 <div class="subnav-helper"></div> <div class="subnav">    <div class="head"><h5>Language Reference</h5> <p class="subnav-duplicate"><a href="../spec/spec.html">table of contents</a></p></div>
   93     <ul>        <li><a href='            ../spec/intro.html'>Introduction</a></li><li><a href='            ../spec/lex.html'>Lexical</a></li><li><a href='            ../spec/grammar.html'>Grammar</a></li><li><a href='            ../spec/module.html'>Modules</a></li><li><a href='            ../spec/declaration.html'>Declarations</a></li><li><a href='            ../spec/type.html'>Types</a></li><li><a href='            ../spec/property.html'>Properties</a></li><li><a href='            ../spec/attribute.html'>Attributes</a></li><li><a href='            ../spec/pragma.html'>Pragmas</a></li><li><a href='            ../spec/expression.html'>Expressions</a></li><li><a href='            ../spec/statement.html'>Statements</a></li><li><a href='            ../spec/arrays.html'>Arrays</a></li><li><a href='            ../spec/hash-map.html'>Associative Arrays</a></li><li><a href='            ../spec/struct.html'>Structs and Unions</a></li><li><a href='            ../spec/class.html'>Classes</a></li><li><a href='            ../spec/interface.html'>Interfaces</a></li><li><a href='            ../spec/enum.html'>Enums</a></li><li><a href='            ../spec/const3.html'>Type Qualifiers</a></li><li><a href='            ../spec/function.html'>Functions</a></li><li><a href='            ../spec/operatoroverloading.html'>Operator Overloading</a></li><li><a href='            ../spec/template.html'>Templates</a></li><li><a href='            ../spec/template-mixin.html'>Template Mixins</a></li><li><a href='            ../spec/contracts.html'>Contract Programming</a></li><li><a href='            ../spec/version.html'>Conditional Compilation</a></li><li><a href='            ../spec/traits.html'>Traits</a></li><li><a href='            ../spec/errors.html'>Error Handling</a></li><li><a href='            ../spec/unittest.html'>Unit Tests</a></li><li><a href='            ../spec/garbage.html'>Garbage Collection</a></li><li><a href='            ../spec/float.html'>Floating Point</a></li><li><a href='            ../spec/iasm.html'>D x86 Inline Assembler</a></li><li><a href='            ../spec/ddoc.html'>Embedded Documentation</a></li><li><a href='            ../spec/interfaceToC.html'>Interfacing to C</a></li><li><a href='            ../spec/cpp_interface.html'>Interfacing to C++</a></li><li><a href='            ../spec/objc_interface.html'>Interfacing to Objective-C</a></li><li><a href='            ../spec/portability.html'>Portability Guide</a></li><li><a href='            ../spec/entity.html'>Named Character Entities</a></li><li><a href='            ../spec/memory-safe-d.html'>Memory Safety</a></li><li><a href='            ../spec/abi.html'>Application Binary Interface</a></li><li><a href='            ../spec/simd.html'>Vector Extensions</a></li><li><a href='            ../spec/betterc.html'>Better C</a></li><li><a href='            ../spec/ob.html'>Live Functions
   94         </a></li>
   95     </ul>
   96 </div>
   97     <div class="hyphenate" id="content">        
   98 <div id="tools"><div >  <div class="tip smallprint">        <a href="https://issues.dlang.org/enter_bug.cgi?bug_file_loc=http%3A%2F%2Fdlang.org/&amp;component=dlang.org&amp;op_sys=All&amp;priority=P3&amp;product=D&amp;rep_platform=All&amp;short_desc=%5BTemplate Mixins%5D&amp;version=D2&amp;bug_severity=enhancement">Report a bug</a>
   99         <div >          If you spot a problem with this page, click here to create a Bugzilla issue.
  100         </div>
  101     </div>
  102     <div class="tip smallprint">        <a href="https://github.com/dlang/dlang.org/edit/master/spec/template-mixin.dd">Improve this page</a>
  103         <div >          Quickly fork, edit online, and submit a pull request for this page.
  104             Requires a signed-in GitHub account. This works well for small changes.
  105             If you'd like to make larger changes you may want to consider using
  106             a local clone.
  107         </div>
  108     </div>
  109 </div></div>
  110         <h1>Template Mixins</h1>
  111         
  112 <style>    body { counter-reset: h1 22; counter-increment: h1 -1; }
  113     h1 { counter-reset: h2 h3 p; }
  114     h2 { counter-reset: h3 h4 p; }
  115     h3 { counter-reset: h4 p; }
  116     h4 { counter-reset: p; }
  117     h1::before {
  118         counter-increment: h1;
  119         content: counter(h1) ". ";
  120     }
  121     h2::before {
  122         counter-increment: h2;
  123         content: counter(h1) "." counter(h2) " ";
  124     }
  125     h3::before {
  126         counter-increment: h3;
  127         content: counter(h1) "." counter(h2) "." counter(h3) " ";
  128     }
  129     h4::before {
  130         counter-increment: h4;
  131         content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " ";
  132     }
  133     p::before, .spec-boxes::before {
  134         counter-increment: p;
  135         content: counter(p) ". ";
  136     }
  137     h1::before, h2::before, h3::before, h4::before, p::before, .spec-boxes::before
  138     {
  139         color: #999;
  140         font-size: 80%;
  141         margin-right: 0.25em;
  142     }
  143 </style>
  144         <div class="blankline"></div>
  145 <div class="blankline"></div>
  146 <div class="blankline"></div>
  147 <div class="page-contents quickindex">    <div class="page-contents-header">        <b>Contents</b>
  148     </div>
  149     <ol>    <li><a href="#mixin_scope">Mixin Scope</a></li>
  150 </ol>
  151 </div>
  152 <div class="blankline"></div>
  153         <p>A <i>TemplateMixin</i> takes an arbitrary set of declarations from
  154         the body of a <i>TemplateDeclaration</i> and inserts them
  155         into the current context.</p>
  156 <div class="blankline"></div>
  157 <pre class="bnf notranslate"><a id="TemplateMixinDeclaration"><span class="gname">TemplateMixinDeclaration</span></a>:
  158     <span class="d_inlinecode donthyphenate notranslate">mixin</span> <span class="d_inlinecode donthyphenate notranslate">template</span> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <a href="../spec/template.html#TemplateParameters"><i>TemplateParameters</i></a> <a href="../spec/template.html#Constraint"><i>Constraint</i></a><sub>opt</sub> <span class="d_inlinecode donthyphenate notranslate">{</span> <a href="../spec/module.html#DeclDefs"><i>DeclDefs</i></a><sub>opt</sub> <span class="d_inlinecode donthyphenate notranslate">}</span>
  159 <div class="blankline"></div>
  160 <a id="TemplateMixin"><span class="gname">TemplateMixin</span></a>:
  161     <span class="d_inlinecode donthyphenate notranslate">mixin</span> <a href="#MixinTemplateName"><i>MixinTemplateName</i></a> <a href="../spec/template.html#TemplateArguments"><i>TemplateArguments</i></a><sub>opt</sub> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a><sub>opt</sub> <span class="d_inlinecode donthyphenate notranslate">;</span>
  162 <div class="blankline"></div>
  163 <a id="MixinTemplateName"><span class="gname">MixinTemplateName</span></a>:
  164     <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="#QualifiedIdentifierList"><i>QualifiedIdentifierList</i></a>
  165     <a href="#QualifiedIdentifierList"><i>QualifiedIdentifierList</i></a>
  166     <a href="../spec/declaration.html#Typeof"><i>Typeof</i></a> <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="#QualifiedIdentifierList"><i>QualifiedIdentifierList</i></a>
  167 <div class="blankline"></div>
  168 <a id="QualifiedIdentifierList"><span class="gname">QualifiedIdentifierList</span></a>:
  169     <a href="../spec/lex.html#Identifier"><i>Identifier</i></a>
  170     <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <span class="d_inlinecode donthyphenate notranslate">.</span> <i>QualifiedIdentifierList</i>
  171     <a href="../spec/template.html#TemplateInstance"><i>TemplateInstance</i></a> <span class="d_inlinecode donthyphenate notranslate">.</span> <i>QualifiedIdentifierList</i>
  172 </pre>
  173 <div class="blankline"></div>
  174         <p>A <i>TemplateMixin</i> can occur in declaration lists of modules,
  175         classes, structs, unions, and as a statement.
  176         The <i>MixinTemplateName</i> refers to a <i>TemplateDeclaration</i> or
  177         <i>TemplateMixinDeclaration</i>.
  178         If the <i>TemplateDeclaration</i> has no parameters, the mixin
  179         form that has no !(<i>TemplateArgumentList</i>)
  180         can be used.
  181         </p>
  182 <div class="blankline"></div>
  183         <p>Unlike a template instantiation, a template mixin's body is evaluated
  184         within the scope where the mixin appears, not where the template declaration
  185         is defined. It is analogous to cutting and pasting the body of
  186         the template into the location of the mixin into a <a href="#mixin_scope">nested scope</a>. It is useful for injecting
  187         parameterized &lsquo;boilerplate&rsquo; code, as well as for creating
  188         templated nested functions, which is not possible with
  189         template instantiations.
  190         </p>
  191 <div class="blankline"></div>
  192 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  193 {
  194     <span class="d_keyword">int</span> x = 5;
  195 }
  196 
  197 <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  198 
  199 <span class="d_keyword">struct</span> Bar
  200 {
  201     <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  202 }
  203 
  204 <span class="d_keyword">void</span> test()
  205 {
  206     writefln(<span class="d_string">"x = %d"</span>, x);         <span class="d_comment">// prints 5
  207 </span>    {
  208         Bar b;
  209         <span class="d_keyword">int</span> x = 3;
  210 
  211         writefln(<span class="d_string">"b.x = %d"</span>, b.x); <span class="d_comment">// prints 5
  212 </span>        writefln(<span class="d_string">"x = %d"</span>, x);     <span class="d_comment">// prints 3
  213 </span>        {
  214             <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  215             writefln(<span class="d_string">"x = %d"</span>, x); <span class="d_comment">// prints 5
  216 </span>            x = 4;
  217             writefln(<span class="d_string">"x = %d"</span>, x); <span class="d_comment">// prints 4
  218 </span>        }
  219         writefln(<span class="d_string">"x = %d"</span>, x);     <span class="d_comment">// prints 3
  220 </span>    }
  221     writefln(<span class="d_string">"x = %d"</span>, x);         <span class="d_comment">// prints 5
  222 </span>}
  223 </pre>
  224 <div class="blankline"></div>
  225         <p>Mixins can be parameterized:</p>
  226 <div class="blankline"></div>
  227 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo(T)
  228 {
  229     T x = 5;
  230 }
  231 
  232 <b><i><span class="d_keyword">mixin</span> Foo!(<span class="d_keyword">int</span>);</i></b>           <span class="d_comment">// create x of type int
  233 </span></pre>
  234 <div class="blankline"></div>
  235         <p>Mixins can add virtual functions to a class:</p>
  236 <div class="blankline"></div>
  237 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  238 {
  239     <span class="d_keyword">void</span> func() { writeln(<span class="d_string">"Foo.func()"</span>); }
  240 }
  241 
  242 <span class="d_keyword">class</span> Bar
  243 {
  244     <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  245 }
  246 
  247 <span class="d_keyword">class</span> Code : Bar
  248 {
  249     <span class="d_keyword">override</span> <span class="d_keyword">void</span> func() { writeln(<span class="d_string">"Code.func()"</span>); }
  250 }
  251 
  252 <span class="d_keyword">void</span> test()
  253 {
  254     Bar b = <span class="d_keyword">new</span> Bar();
  255     b.func();      <span class="d_comment">// calls Foo.func()
  256 </span>
  257     b = <span class="d_keyword">new</span> Code();
  258     b.func();      <span class="d_comment">// calls Code.func()
  259 </span>}
  260 </pre>
  261 <div class="blankline"></div>
  262         <p>Mixins are evaluated in the scope of where they appear, not the scope
  263         of the template declaration:</p>
  264 <div class="blankline"></div>
  265 <pre class="d_code notranslate"><span class="d_keyword">int</span> y = 3;
  266 
  267 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  268 {
  269     <span class="d_keyword">int</span> abc() { <span class="d_keyword">return</span> y; }
  270 }
  271 
  272 <span class="d_keyword">void</span> test()
  273 {
  274     <span class="d_keyword">int</span> y = 8;
  275     <b><i><span class="d_keyword">mixin</span> Foo;</i></b> <span class="d_comment">// local y is picked up, not global y
  276 </span>    <span class="d_keyword">assert</span>(abc() == 8);
  277 }
  278 </pre>
  279 <div class="blankline"></div>
  280         <p>Mixins can parameterize symbols using alias parameters:</p>
  281 <div class="blankline"></div>
  282 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo(<span class="d_keyword">alias</span> b)
  283 {
  284     <span class="d_keyword">int</span> abc() { <span class="d_keyword">return</span> b; }
  285 }
  286 
  287 <span class="d_keyword">void</span> test()
  288 {
  289     <span class="d_keyword">int</span> y = 8;
  290     <b><i><span class="d_keyword">mixin</span> Foo!(y);</i></b>
  291     <span class="d_keyword">assert</span>(abc() == 8);
  292 }
  293 </pre>
  294 <div class="blankline"></div>
  295         <p>This example uses a mixin to implement a generic Duff's device
  296         for an arbitrary statement (in this case, the arbitrary statement
  297         is in bold). A nested function is generated as well as a
  298         delegate literal, these can be inlined by the compiler:</p>
  299 <div class="blankline"></div>
  300 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> duffs_device(<span class="d_keyword">alias</span> id1, <span class="d_keyword">alias</span> id2, <span class="d_keyword">alias</span> s)
  301 {
  302     <span class="d_keyword">void</span> duff_loop()
  303     {
  304         <span class="d_keyword">if</span> (id1 &lt; id2)
  305         {
  306             <span class="d_keyword">typeof</span>(id1) n = (id2 - id1 + 7) / 8;
  307             <span class="d_keyword">switch</span> ((id2 - id1) % 8)
  308             {
  309                 <span class="d_keyword">case</span> 0: <span class="d_keyword">do</span> { s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  310                 <span class="d_keyword">case</span> 7:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  311                 <span class="d_keyword">case</span> 6:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  312                 <span class="d_keyword">case</span> 5:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  313                 <span class="d_keyword">case</span> 4:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  314                 <span class="d_keyword">case</span> 3:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  315                 <span class="d_keyword">case</span> 2:      s(); <span class="d_keyword">goto</span> <span class="d_keyword">case</span>;
  316                 <span class="d_keyword">case</span> 1:      s(); <span class="d_keyword">continue</span>;
  317                 <span class="d_keyword">default</span>:     <span class="d_keyword">assert</span>(0, <span class="d_string">"Impossible"</span>);
  318                         } <span class="d_keyword">while</span> (--n &gt; 0);
  319             }
  320         }
  321     }
  322 }
  323 
  324 <span class="d_keyword">void</span> foo() { writeln(<span class="d_string">"foo"</span>); }
  325 
  326 <span class="d_keyword">void</span> test()
  327 {
  328     <span class="d_keyword">int</span> i = 1;
  329     <span class="d_keyword">int</span> j = 11;
  330 
  331     <span class="d_keyword">mixin</span> duffs_device!(i, j, <b><i><span class="d_keyword">delegate</span> { foo(); }</i></b>);
  332     duff_loop();  <span class="d_comment">// executes foo() 10 times
  333 </span>}
  334 </pre>
  335 <div class="blankline"></div>
  336 <h2><a class="anchor" title="Permalink to this section" id="mixin_scope" href="#mixin_scope">Mixin Scope</a></h2>
  337 <div class="blankline"></div>
  338         <p>The declarations in a mixin are placed in a nested scope and then
  339         &lsquo;imported&rsquo; into the surrounding
  340         scope. If the name of a declaration in a mixin is the same
  341         as a declaration in the surrounding scope, the surrounding declaration
  342         overrides the mixin one:</p>
  343 <div class="blankline"></div>
  344 <pre class="d_code notranslate"><span class="d_keyword">int</span> x = 3;
  345 
  346 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  347 {
  348     <span class="d_keyword">int</span> x = 5;
  349     <span class="d_keyword">int</span> y = 5;
  350 }
  351 
  352 <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  353 <span class="d_keyword">int</span> y = 3;
  354 
  355 <span class="d_keyword">void</span> test()
  356 {
  357     writefln(<span class="d_string">"x = %d"</span>, x);  <span class="d_comment">// prints 3
  358 </span>    writefln(<span class="d_string">"y = %d"</span>, y);  <span class="d_comment">// prints 3
  359 </span>}
  360 </pre>
  361 <div class="blankline"></div>
  362         <p>If two different mixins are put in the same scope, and each
  363         define a declaration with the same name, there is an ambiguity
  364         error when the declaration is referenced:</p>
  365 <div class="blankline"></div>
  366 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  367 {
  368     <span class="d_keyword">int</span> x = 5;
  369     <span class="d_keyword">void</span> func(<span class="d_keyword">int</span> x) { }
  370 }
  371 
  372 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Bar()
  373 {
  374     <span class="d_keyword">int</span> x = 4;
  375     <span class="d_keyword">void</span> func(<span class="d_keyword">long</span> x) { }
  376 }
  377 
  378 <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  379 <b><i><span class="d_keyword">mixin</span> Bar;</i></b>
  380 
  381 <span class="d_keyword">void</span> test()
  382 {
  383     <span class="d_keyword">import</span> std.stdio : writefln;
  384     writefln(<span class="d_string">"x = %d"</span>, x); <span class="d_comment">// error, x is ambiguous
  385 </span>    func(1);               <span class="d_comment">// error, func is ambiguous
  386 </span>}
  387 </pre>
  388         <p>The call to <span class="d_inlinecode donthyphenate notranslate">func()</span> is ambiguous because
  389         Foo.func and Bar.func are in different scopes.
  390         </p>
  391 <div class="blankline"></div>
  392         <p>If a mixin has an <i>Identifier</i>, it can be used to
  393         disambiguate between conflicting symbols:
  394         </p>
  395 <pre class="d_code notranslate"><span class="d_keyword">int</span> x = 6;
  396 
  397 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  398 {
  399     <span class="d_keyword">int</span> x = 5;
  400     <span class="d_keyword">int</span> y = 7;
  401     <span class="d_keyword">void</span> func() { }
  402 }
  403 
  404 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Bar()
  405 {
  406     <span class="d_keyword">int</span> x = 4;
  407     <span class="d_keyword">void</span> func() { }
  408 }
  409 
  410 <b><i><span class="d_keyword">mixin</span> Foo F;</i></b>
  411 <b><i><span class="d_keyword">mixin</span> Bar B;</i></b>
  412 
  413 <span class="d_keyword">void</span> test()
  414 {
  415     writefln(<span class="d_string">"y = %d"</span>, y);     <span class="d_comment">// prints 7
  416 </span>    writefln(<span class="d_string">"x = %d"</span>, x);     <span class="d_comment">// prints 6
  417 </span>    writefln(<span class="d_string">"F.x = %d"</span>, F.x); <span class="d_comment">// prints 5
  418 </span>    writefln(<span class="d_string">"B.x = %d"</span>, B.x); <span class="d_comment">// prints 4
  419 </span>    F.func();                  <span class="d_comment">// calls Foo.func
  420 </span>    B.func();                  <span class="d_comment">// calls Bar.func
  421 </span>}
  422 </pre>
  423         <p>Alias declarations can be used to overload together
  424         functions declared in different mixins:</p>
  425 <div class="blankline"></div>
  426 <pre class="d_code notranslate"><span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  427 {
  428     <span class="d_keyword">void</span> func(<span class="d_keyword">int</span> x) {  }
  429 }
  430 
  431 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Bar()
  432 {
  433     <span class="d_keyword">void</span> func(<span class="d_keyword">long</span> x) {  }
  434 }
  435 
  436 <span class="d_keyword">mixin</span> Foo!() F;
  437 <span class="d_keyword">mixin</span> Bar!() B;
  438 
  439 <b><i><span class="d_keyword">alias</span> func = F.func;</i></b>
  440 <b><i><span class="d_keyword">alias</span> func = B.func;</i></b>
  441 
  442 <span class="d_keyword">void</span> main()
  443 {
  444     func(1);  <span class="d_comment">// calls B.func
  445 </span>    func(1L); <span class="d_comment">// calls F.func
  446 </span>}
  447 </pre>
  448 <div class="blankline"></div>
  449 <div class="blankline"></div>
  450         <p>A mixin has its own scope, even if a declaration is overridden
  451         by the enclosing one:</p>
  452 <div class="blankline"></div>
  453 <pre class="d_code notranslate"><span class="d_keyword">int</span> x = 4;
  454 
  455 <span class="d_keyword">mixin</span> <span class="d_keyword">template</span> Foo()
  456 {
  457     <span class="d_keyword">int</span> x = 5;
  458     <span class="d_keyword">int</span> bar() { <span class="d_keyword">return</span> x; }
  459 }
  460 
  461 <b><i><span class="d_keyword">mixin</span> Foo;</i></b>
  462 
  463 <span class="d_keyword">void</span> test()
  464 {
  465     writefln(<span class="d_string">"x = %d"</span>, x);         <span class="d_comment">// prints 4
  466 </span>    writefln(<span class="d_string">"bar() = %d"</span>, bar()); <span class="d_comment">// prints 5
  467 </span>}
  468 </pre>
  469 <div class="blankline"></div>
  470 
  471 <div style="float: left"><i class="fa fa-angle-left" aria-hidden="true"></i> <a href="../spec/template.html">Templates</a></div>
  472 <div style="float: right"><a href="../spec/contracts.html">Contract Programming</a> <i class="fa fa-angle-right" aria-hidden="true"></i></div>
  473 <div style="clear:both"></div>
  474 
  475 
  476 
  477         <div class="smallprint" id="copyright">Copyright &copy; 1999-2020 by the <a href="../foundation_overview.html">D Language Foundation</a> | Page generated by
  478 <a href="../spec/ddoc.html">Ddoc</a> on Fri Nov 20 21:58:02 2020
  479 </div>
  480     </div>
  481 </div>
  482 
  483     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  484     <script type="text/javascript">window.jQuery || document.write('\x3Cscript src="../js/jquery-1.7.2.min.js">\x3C/script>');</script>
  485     <script type="text/javascript" src="../js/dlang.js"></script>
  486     
  487     <script type="text/javascript" src="../js/codemirror-compressed.js"></script>
  488     <script type="text/javascript" src="../js/run.js"></script>
  489 
  490 
  491 <script type="text/javascript" src="../js/listanchors.js"></script>
  492 <script type="text/javascript">jQuery(document).ready(addVersionSelector);</script>
  493 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  494 </body>
  495 </html>