View mode: basic / threaded / horizontal-split · Log in · Help
January 10, 2006
Variadic function arguments and build in variant type
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
January 11, 2006
Re: Variadic function arguments and build in variant type
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
January 11, 2006
Re: Variadic function arguments and build in variant type
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
Top | Discussion index | About this forum | D home