<a href="http://www.reddit.com/r/d_language/comments/lghzs/how_do_you_use_templates_in_d/">ntrel2</a> has the answer:<div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">char genChar() {</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>return cast(char) uniform(0, 128);</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">T[] genArray(T)(T function() gen) {</font></div><div>

<font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>int len = uniform(0, 100);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>T[] arr = [];</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">      </span>for(int i = 0; i < len; i++) {</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">             </span>arr ~= gen();</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>}</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">      </span>return arr;</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">string genString() {</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>return genArray(&genChar).idup;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div>

<div><br></div><div>Now I'm curious: How can I pass and accept a collection of lambdas? The lambdas will each have different, arbitrary returns types. And how do I call a function using a collection of values, something like Lisp's (apply f args) ?</div>

<div><br></div>Cheers,<div><br></div><div>Andrew Pennebaker</div><div><a href="http://www.yellosoft.us" target="_blank">www.yellosoft.us</a></div>
<br><div class="gmail_quote">On Tue, Oct 18, 2011 at 1:56 PM, Andrew Pennebaker <span dir="ltr"><<a href="mailto:andrew.pennebaker@gmail.com">andrew.pennebaker@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div>I'm writing a function called <font face="'courier new', monospace">genArray</font> that accepts a function <font face="'courier new', monospace">gen</font> and returns a random array populated by calling <font face="'courier new', monospace">gen()</font>. genString builds on this by returning <font face="'courier new', monospace">genArray(&genChar)</font>. But my type signatures are wrong.</div>


<div><br></div><div>In dashcheck.d:</div><div><br></div><div><font face="'courier new', monospace">char genChar() {</font></div><div><font face="'courier new', monospace">    return cast(char) uniform(0, 128);</font></div>


<div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">T[] genArray(T function() gen) {</font></div>


<div><font face="'courier new', monospace">    int len = uniform(0, 100);</font></div><div><font face="'courier new', monospace">    T[] arr = [];</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">    for(int i = 0; i < len; i++) {</font></div><div>
<font face="'courier new', monospace">        arr ~= gen();</font></div><div><font face="'courier new', monospace">    }</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">    return arr;</font></div><div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">string genString() {</font></div><div><font face="'courier new', monospace">    return genArray(&genChar);</font></div>
<div><font face="'courier new', monospace">}</font></div><div><br></div><div>Trace:</div><div><br></div><div><font face="'courier new', monospace">dashcheck.d(17): Error: undefined identifier T</font></div>


<div><font face="'courier new', monospace">dashcheck.d(17): Error: T is used as a type</font></div><div><font face="'courier new', monospace">dashcheck.d(17): Error: undefined identifier T</font></div>
<div><font face="'courier new', monospace">dashcheck.d(17): Error: T is used as a type</font></div><div><br></div><div>Full code at <a href="https://github.com/mcandre/dashcheck" target="_blank">GitHub</a></div>
<div><br></div>Cheers,<div><br></div><font color="#888888"><div>Andrew Pennebaker</div><div><a href="http://www.yellosoft.us" target="_blank">www.yellosoft.us</a></div>
</font></blockquote></div><br></div>