MakeArray Template for loading a Dynamic Array example:
/+
' Source : MakeArray.d
' Authors : Eric 'pragma' Anderton and Andy Friesen (for the original version)
' Created : 17.Jul.04, modified 21.Mar.05
' Modified: 19.Jul.06 recompiled and tested with dmd v0.163.
' : 05.Nov.06 recompiled and tested with dmd v0.173.
' Ref : 21.Mar.05 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=130
' : 17.Jul.04 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=6363
+/
private import std.stdio;
private import std.string;
private import std.stdarg;
template makeArray(T)
{
T[] makeArray(...)
{
T[] result;
result.length = _arguments.length;
for( int i = 0; i < _arguments.length; i++ )
{
//writefln("typeif(T)=%s, _arguments[%d]=%s",
// typeid(T).toString(), i, _arguments[i].toString() );
if ( _arguments[i] is typeid(T) )
result[i] = va_arg!(T)(_argptr);
else
throw new Exception(format("(makeArray) element %d is not of type %s.",
toString(i + 1), typeid(T).toString()));
}
return result;
}
}
int main()
{
char[][] sArr = makeArray!(char[])("one", "two", "three", "four");
// Note: The Real literals needed a decimal point and the "L" before they would work.
// Also "U" is used for unsigned, "L" which has a double meaning...it means "Long"
// for whole numbers and "real" for floating-point, and then there's "f" for a float.
real r = 12_000.99L;
real[] rArr = makeArray!(real)(-70_000.10L, 12_000.99L, r, 15_000.00L, 18_123.55L, 21_099.01L);
long[] lArr = makeArray!(long)(-3_997L, 98_675L, 231_756L, long.max, long.min);
ulong[] ulArr = makeArray!(ulong)(3_997UL, 98_675UL, 231_756UL, ulong.max, ulong.min);
float[] fArr = makeArray!(float)(3_997.1f, 98_675.3f, 231_756.4f, 18_123.55f, 21_099.01f);
double[] dArr = makeArray!(double)(3_997.1, 98_675.3, 231_756.4, 18_123.55, 21_099.01);
int[] iArr = makeArray!(int)(-3_997, 98_675, 231_756, int.max, int.min);
uint[] uiArr = makeArray!(uint)(3_997U, 98_675U, 231_756U, uint.max, uint.min);
writefln("List of the Strings.");
for ( int i = 0; i < sArr[][].length; i++ )
writefln("sArr[%d]=\"%s\"", i, sArr[i]);
writefln();
writefln("List of the Reals.");
for ( int i = 0; i < rArr[].length; i++ )
writefln("rArr[%d]=%10.2f", i, rArr[i]);
writefln();
writefln("List of the Floats.");
for ( int i = 0; i < fArr[].length; i++ )
writefln("fArr[%d]=%10.2f", i, fArr[i]);
writefln();
writefln("List of the Doubles.");
for ( int i = 0; i < fArr[].length; i++ )
writefln("dArr[%d]=%10.2f", i, dArr[i]);
writefln();
writefln("List of the Longs.");
for ( int i = 0; i < lArr[].length; i++ )
writefln("lArr[%d]=%d", i, lArr[i]);
writefln();
writefln("List of the ULongs.");
for ( int i = 0; i < ulArr[].length; i++ )
writefln("ulArr[%d]=%d", i, ulArr[i]);
writefln();
writefln("List of the Ints.");
for ( int i = 0; i < iArr[].length; i++ )
writefln("iArr[%d]=%d", i, iArr[i]);
writefln();
writefln("List of the UInts.");
for ( int i = 0; i < uiArr[].length; i++ )
writefln("uiArr[%d]=%d", i, uiArr[i]);
return 0;
}
C:\dmd>dmd makearray.d
C:\dmd\bin\..\..\dm\bin\link.exe makearray,,,user32+kernel32/noi;
C:\dmd>makearray
List of the Strings.
sArr[0]="one"
sArr[1]="two"
sArr[2]="three"
sArr[3]="four"
List of the Reals.
rArr[0]= -70000.10
rArr[1]= 12000.99
rArr[2]= 12000.99
rArr[3]= 15000.00
rArr[4]= 18123.55
rArr[5]= 21099.01
List of the Floats.
fArr[0]= 3997.10
fArr[1]= 98675.30
fArr[2]= 231756.41
fArr[3]= 18123.55
fArr[4]= 21099.01
List of the Doubles.
dArr[0]= 3997.10
dArr[1]= 98675.30
dArr[2]= 231756.40
dArr[3]= 18123.55
dArr[4]= 21099.01
List of the Longs.
lArr[0]=-3997
lArr[1]=98675
lArr[2]=231756
lArr[3]=9223372036854775807
lArr[4]=-9223372036854775808
List of the ULongs.
ulArr[0]=3997
ulArr[1]=98675
ulArr[2]=231756
ulArr[3]=18446744073709551615
ulArr[4]=0
List of the Ints.
iArr[0]=-3997
iArr[1]=98675
iArr[2]=231756
iArr[3]=2147483647
iArr[4]=-2147483648
List of the UInts.
uiArr[0]=3997
uiArr[1]=98675
uiArr[2]=231756
uiArr[3]=4294967295
uiArr[4]=0
C:\dmd>