<br><br><div class="gmail_quote">On Sat, Jan 23, 2010 at 14:44, Simen kjaeraas <span dir="ltr"><<a href="mailto:simen.kjaras@gmail.com">simen.kjaras@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
In attempting to create a function to initialize any array of structs in a simple manner, I created this code:<br>
<br>
void fillArr( uint n, T, U... )( ref T[] arr, U args ) {<br>
arr[0] = T( U[0..n] );<br>
static if ( U.length > n ) {<br>
fillArr!( n )( arr[ 1..$ ], args[ n..$ ] );<br>
}<br>
}<br></blockquote><div><br>U is a type . U[0..n] is also a type. You should write:<br><br>arr[0] = T(args[0..n]);<br><br><br>Maybe you could also use S.tupleof.length to get the number of fields in S (or maybe there is a __traits which gives this) and avoid the recursion in initArray:<br>
<br><br>void fillArr( T, U... )( ref T[] arr, U args ) {<br> arr[0] = T(args[0..T.tupleof.length]) ;<br> static if ( U.length > T.tupleof.length ) {<br> fillArr( arr[ 1..$ ], args[ T.tupleof.length..$ ] );<br> }<br>
}<br><br>T[] initArray( T, U... )( U args )<br> if ( U.length > 0<br> && (is(typeof( T( args[ 0..T.tupleof.length ] ) ) ) )<br> && ( U.length % T.tupleof.length == 0 ))<br>{<br> T[] result = new T[ U.length / T.tupleof.length ]; // Create an array<br>
fillArr( result, args ); // and fill it.<br> return result;<br>}<br><br><font color="#888888">
</font></div></div>Philippe<br><br>