Thread overview
Using variable argument lists in D
Apr 22, 2004
Pasi Hirvonen
Apr 22, 2004
C. Sauls
April 22, 2004
Hello.

First of all, please smack me and direct me to the correct page if the reference documentation actually covers this and I missed it.

What is the proper way to handle variable argument lists
in D? Phobos' std stuff seems to use va_list stuff but
guessing the Right Way is bit annoying.

I probably want to call C functions such as vfprintf() when using them too, but I doubt that that it's going to be a problem.

Thanks in advance
April 22, 2004
As far as I know, va_list is the way to do it (complete with func(...) syntax).  There are a few experimental systems out there using templates, but I haven't personally played with any of them.  Of course if all your arguments are of the same type, you could use an array... (Which will be much more usable once array literals are implemented.)

-C. Sauls
-Invironz

Pasi Hirvonen wrote:
> Hello.
> 
> First of all, please smack me and direct me to the correct
> page if the reference documentation actually covers this
> and I missed it.
> 
> What is the proper way to handle variable argument lists in D? Phobos' std stuff seems to use va_list stuff but
> guessing the Right Way is bit annoying.
> 
> I probably want to call C functions such as vfprintf() when
> using them too, but I doubt that that it's going to be a
> problem.
> 
> Thanks in advance
April 22, 2004
C. Sauls wrote:

> As far as I know, va_list is the way to do it (complete with func(...) syntax).  There are a few experimental systems out there using templates, but I haven't personally played with any of them.  Of course if all your arguments are of the same type, you could use an array... (Which will be much more usable once array literals are implemented.)
> 
> -C. Sauls
> -Invironz
> 
> Pasi Hirvonen wrote:
> 
>> Hello.
>>
>> First of all, please smack me and direct me to the correct
>> page if the reference documentation actually covers this
>> and I missed it.
>>
>> What is the proper way to handle variable argument lists in D? Phobos' std stuff seems to use va_list stuff but
>> guessing the Right Way is bit annoying.
>>
>> I probably want to call C functions such as vfprintf() when
>> using them too, but I doubt that that it's going to be a
>> problem.
>>
>> Thanks in advance

I think the best way for D argument lists is with a variant type: all arguments passed where va arguments are expected are promoted to type 'variant' and then by the use of RTTI one can ask what the type of the variant is and act accordingly. The va_list could be presented as an array of variants which is a static property of the function.

Example:


void printf(char[] format, ...)
{
    for(int i = 0; i < printf.args.size; i++)
    {
        switch (printf.args[i].type)
	{
	    case int.type:
                break;

            case char[].type:
                break;
        }
    }
}