www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variadic function arguments and build in variant type

reply aarti_pl <aarti_pl_member pathlink.com> writes:
Hello!  

I am new here and at the beginning I would like to say thanks to all for D  
language. I hope it will gain in popularity because it is really great!  

I would like to ask if there are consideration to make build in variant type.  
In such a case there could be much more cleaner solution to send variadic  
arguments to functions.  

Instead of using:  
---------  
void foo(int x, ...)  
{  
printf("%d arguments\n", _arguments.length);  
for (int i = 0; i < _arguments.length; i++)  
{   _arguments[i].print();  

if (_arguments[i] == typeid(int))  
...  
---------  

you could just write:  
---------  
void foo(variant[] arg ...)  
{  
printf("%d arguments\n", arg.length);  
for (int i = 0; i < arg.length; i++)  
{    
if (arg[i].type == int)  // I am not sure of this how should it   
//be expressed in D 
...  
---------  

I think this solution is much more consistent and simpler to use. What's more  
it gives extra bonus of having built in variant type, which is not so easy to  
implement in such a languages as C++.  

What do you think?  

Best Regards  
Aarti  
Jan 10 2006
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
aarti_pl wrote:
 Hello!  
 
 I am new here and at the beginning I would like to say thanks to all for D  
 language. I hope it will gain in popularity because it is really great!  
 
 I would like to ask if there are consideration to make build in variant type.  
 In such a case there could be much more cleaner solution to send variadic  
 arguments to functions.  
 
 Instead of using:  
 ---------  
 void foo(int x, ...)  
 {  
 printf("%d arguments\n", _arguments.length);  
 for (int i = 0; i < _arguments.length; i++)  
 {   _arguments[i].print();  
 
 if (_arguments[i] == typeid(int))  
 ....  
 ---------  
 
 you could just write:  
 ---------  
 void foo(variant[] arg ...)  
 {  
 printf("%d arguments\n", arg.length);  
 for (int i = 0; i < arg.length; i++)  
 {    
 if (arg[i].type == int)  // I am not sure of this how should it   
 //be expressed in D 
 ....  
 ---------  
 
 I think this solution is much more consistent and simpler to use. What's more  
 it gives extra bonus of having built in variant type, which is not so easy to  
 implement in such a languages as C++.  
 
 What do you think?  
 
 Best Regards  
 Aarti  
 
 

You can already use std.boxer to achieve this -- although if I remember right its still fairly far from perfect. Perhaps these just needs more vocal attention? # import std.stdio, std.boxer; # # void foo (...) { # Box[] args = boxArray(_arguments, _argptr); # writefln(args.length, " arguments"); # foreach (size_t i, inout Box x; args) { # writef("[%d] ", i); # if (unboxable!(int)(x)) { # writefln("int( %d )", unbox!(int)(x)); # } # else { # writefln("%s", x.type.toString()); # } # } # } -- Chris Sauls
Jan 11 2006
parent a.c.edwards <a.c.edwards_member pathlink.com> writes:
In article <dq2nqb$1533$1 digitaldaemon.com>, Chris Sauls says...
aarti_pl wrote:
 Hello!  
 
 I am new here and at the beginning I would like to say thanks to all for D  
 language. I hope it will gain in popularity because it is really great!  
 
 I would like to ask if there are consideration to make build in variant type.  
 In such a case there could be much more cleaner solution to send variadic  
 arguments to functions.  
 
 Instead of using:  
 ---------  
 void foo(int x, ...)  
 {  
 printf("%d arguments\n", _arguments.length);  
 for (int i = 0; i < _arguments.length; i++)  
 {   _arguments[i].print();  
 
 if (_arguments[i] == typeid(int))  
 ....  
 ---------  
 
 you could just write:  
 ---------  
 void foo(variant[] arg ...)  
 {  
 printf("%d arguments\n", arg.length);  
 for (int i = 0; i < arg.length; i++)  
 {    
 if (arg[i].type == int)  // I am not sure of this how should it   
 //be expressed in D 
 ....  
 ---------  
 
 I think this solution is much more consistent and simpler to use. What's more  
 it gives extra bonus of having built in variant type, which is not so easy to  
 implement in such a languages as C++.  
 
 What do you think?  
 
 Best Regards  
 Aarti  
 
 

You can already use std.boxer to achieve this -- although if I remember right its still fairly far from perfect. Perhaps these just needs more vocal attention? # import std.stdio, std.boxer; #

std.boxer doesn't seem to be built into phobos. Need to do the following # dmd test c:\dmd\src\phobos\std\boxer to compile for DMD v0.141, v0.142, and v0.143
-- Chris Sauls

Jan 11 2006