<div class="gmail_quote">On Mon, Aug 2, 2010 at 00:28, Ziad <span dir="ltr"><<a href="mailto:hatahet@gmail.com">hatahet@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>If I want a static array:<br>
<br>
int[5] a = [1, 2, 3, 4, 5];<br>
<br>
I'd have to specify the size in advance (5 in this case), which<br>
means that adding extra elements later on would require that the<br>
size be update.<br>
<br>
Is there a way to let the compiler automatically determine the size<br>
based on the argument list?<br>
</blockquote></div><br><div>Would a template-based solution be OK?</div><div><br></div><div><div>import std.stdio, std.traits;</div><div><br></div><div>CommonType!T[T.length] staticArray(T...)(T vals) </div><div> if ((T.length > 0) && is(CommonType!T))</div>
<div>{</div><div> return [vals];</div><div>}</div><div><br></div><div>void main()</div><div>{</div><div> auto a = staticArray(0,1,2,3,4);</div><div> writeln(typeof(a).stringof); // int[5u]</div><div><br></div><div>
auto b = staticArray(3.14);</div><div> writeln(typeof(b).stringof); // double[1u]</div><div><br></div><div> auto mixed = staticArray(0,1,2,3,4, 3.14);</div><div> writeln(typeof(mixed).stringof); // double[6u]</div>
<div>}</div></div><div><br></div><div><br></div><div>Philippe</div><div><br></div>