www.digitalmars.com         C & C++   DMDScript  

D - Using variable argument lists in D

reply Pasi Hirvonen <psh iki.fi> writes:
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
Apr 22 2004
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
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

Apr 22 2004
parent Achilleas Margaritis <axilmar b-online.gr> writes:
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; } } }
Apr 22 2004