Variadic arguments and map

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Nov 20 13:18:05 PST 2007


"Frank Benoit" <keinfarbton at googlemail.com> wrote in message 
news:fhvifa$221n$1 at digitalmars.com...
> Jarrett Billingsley schrieb:
>
>> This looks like how std.format does it too..
>>
>>
>
> Is this a feature missing from the D language?
> Better access to variadic stuff.

Runtime variadics are really kind of hacky in just about any systems 
programming language.  You'll notice that most scripting languages and VM'ed 
languages (Python, Ruby?, Java, .Net) pass instead an array of Object, since 
Object can hold anything.

Runtime variadics are also slow and possibly unsafe, since you have to do 
all your typechecking and dispatch at runtime, and if you don't do it right, 
you could end up with a malfunctioning function (or at the very least, one 
that doesn't follow i.e. the same conversion rules as the language).

A very nice alternative to runtime variadics is to use a variadic template 
function.  This gives you a list of the argument types at compile time, 
giving you the opportunity to write much more efficient code, at the expense 
of code bloat, since you technically now have a different overload of the 
function for every different list of parameter types.  Templated functions 
also can't be virtual.

A nice alternative would be, once D2 gets opImplicitCastFrom (is that what 
it's called?) implemented, to have a Variant type and use a variadic array 
of those:

void func(Variant[] args...)
{
    foreach(arg; args)
    {
        // if it's an int, do something...
    }
}

// implicitly constructs a Variant struct out of each param
func(3, "hi", new Object());

Then you get virtualizability, ease of calling, no code bloat, and even 
though you still have to determine types at runtime, it'll likely be a lot 
more straightforward as a variant type will probably have methods to check 
if it can be cast to certain types, etc. 





More information about the Digitalmars-d mailing list