Is this cool, or what?
yields
It only works for user defined functions, so far. I may submit it so you-all can play with it, but I'd like it to work for built-in functions, too. I'd like a min() and max(), for example, that work with any number of args - just like I demonstarted with the ASH program.
Code:
void test( int ... args )
{
print( "arg count = " + count( args ) );
foreach index, arg in args {
print( "arg(" + index + ") = " + arg );
}
}
// Test calling the vararg function with an array
int [] zero = {};
int [] one = { 1 };
int [] two = { 1, 2 };
int [] three = { 1, 2, 3 };
test( zero );
test( one );
test( two );
test( three );
print( "----" );
// Test calling the vararg function with multiple arguments
test();
test( 1 );
test( 1, 2 );
test( 1, 2, 3 );
print( "----" );
int min( int first, int ... args )
{
int retval = first;
foreach index, arg in args {
if ( arg < retval ) {
retval = arg;
}
}
return retval;
}
print( min( 100 ) );
print( min( 100, 50 ) );
print( min( 100, 50, 200, 25 ) );
Code:
[color=green]> varargs.ash[/color]
arg count = 0
arg count = 1
arg(0) = 1
arg count = 2
arg(0) = 1
arg(1) = 2
arg count = 3
arg(0) = 1
arg(1) = 2
arg(2) = 3
----
arg count = 0
arg count = 1
arg(0) = 1
arg count = 2
arg(0) = 1
arg(1) = 2
arg count = 3
arg(0) = 1
arg(1) = 2
arg(2) = 3
----
100
50
25