D - Variable Args in D
- Kublai Kahn <Kublai_member pathlink.com> Feb 27 2003
- Jonathan Andrew <Jonathan_member pathlink.com> Feb 27 2003
- "Achillefs Margaritis" <axilmar b-online.gr> Feb 27 2003
- Dan Liebgold <Dan_member pathlink.com> Feb 27 2003
- Andy Friesen <andy ikagames.com> Feb 27 2003
- Burton Radons <loth users.sourceforge.net> Feb 27 2003
How is D going to handle variable length function arguments?
It seems like Cs variable argument list is somewhat flawed
and it doesn't support any built in handling of variable length
arguments. It's almost hacked on with va_start and va_end and
you have to include a library.
This is a C program with variable length arguments:
#include <stdio.h>
#include <stdarg.h>
int multintegers(int numbers, ...)
{
va_list list;
int j;
int product;
va_start(list, numbers);
sum = 0;
for (j = 0; j < numbers; j++)
product *= va_arg(list, int);
va_end(list);
return sum;
}
Feb 27 2003
Yeah that definately sucks. Why not be able to accept "args" much the same
way main() can?
i.e.
myfunction(void[] args)
{
}
Then pass it whatever you want, however you want. And then once foreach gets
implemented you can ..... but I'm getting head of myself =).
Perl has the $_[x] array, which is nice to use, but _ for a variable name is
kinda silly if you ask me. Fortunately, args in D isn't a global variable (I
think), so D could use it. It would mean "whatever is passed to the
function" instead of just "whatever the command line arguments are".
-Jon
In article <b3lcao$1com$1 digitaldaemon.com>, Kublai Kahn says...
How is D going to handle variable length function arguments?
It seems like Cs variable argument list is somewhat flawed
and it doesn't support any built in handling of variable length
arguments. It's almost hacked on with va_start and va_end and
you have to include a library.
This is a C program with variable length arguments:
#include <stdio.h>
#include <stdarg.h>
int multintegers(int numbers, ...)
{
va_list list;
int j;
int product;
va_start(list, numbers);
sum = 0;
for (j = 0; j < numbers; j++)
product *= va_arg(list, int);
va_end(list);
return sum;
}
Feb 27 2003
Maybe the following syntax ?
void foo(...)
{
for(int i = 0; i < foo.args.count; i++) {
switch (foo.args[i].type) {
case Int:
break;
case String:
break;
}
}
}
The above example treats the method 'foo' as an object which has a member
'args'. This member 'args' has a member 'count' (number of arguments
passed), it is an array of arguments and each argument has a type.
Since D is a statically compiled language, all this info can be stored on
the stack.
"Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
news:b3m4ni$1rrl$1 digitaldaemon.com...
Yeah that definately sucks. Why not be able to accept "args" much the same
way main() can?
i.e.
myfunction(void[] args)
{
}
Then pass it whatever you want, however you want. And then once foreach
implemented you can ..... but I'm getting head of myself =).
Perl has the $_[x] array, which is nice to use, but _ for a variable name
kinda silly if you ask me. Fortunately, args in D isn't a global variable
think), so D could use it. It would mean "whatever is passed to the
function" instead of just "whatever the command line arguments are".
-Jon
In article <b3lcao$1com$1 digitaldaemon.com>, Kublai Kahn says...
How is D going to handle variable length function arguments?
It seems like Cs variable argument list is somewhat flawed
and it doesn't support any built in handling of variable length
arguments. It's almost hacked on with va_start and va_end and
you have to include a library.
This is a C program with variable length arguments:
#include <stdio.h>
#include <stdarg.h>
int multintegers(int numbers, ...)
{
va_list list;
int j;
int product;
va_start(list, numbers);
sum = 0;
for (j = 0; j < numbers; j++)
product *= va_arg(list, int);
va_end(list);
return sum;
}
Feb 27 2003
In article <b3m4ni$1rrl$1 digitaldaemon.com>, Jonathan Andrew says...Yeah that definately sucks. Why not be able to accept "args" much the same way main() can? i.e. myfunction(void[] args) { } Then pass it whatever you want, however you want. And then once foreach gets implemented you can ..... but I'm getting head of myself =).
The problem is, how do you call "myfunction"? Right now, you can use printf and sprintf very easily... just list out your parameters. In D currently, there is no way to initialize a non-static dynamic array. Perhaps if there was, you could use the syntax you describe above, for example: myfunction( new char*[] = ["parm1", "parm2"] ) Of course you can't vary the parameters types, it is quite ugly, and it requires heap allocation. So what else can be done? And on that topic, what is the right way to do printf style output and string formatting in D? Dan P.S.: being able to initialize non-static dynamic arrays like that would sure be nice.... P.P.S.: isn't it odd how probably the most basic and useful function among C's libraries uses such a hacked together and dangerous techniques?
Feb 27 2003
Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Here's something I'd been playing with. Got the idea from boost::format Dan Liebgold wrote:In article <b3m4ni$1rrl$1 digitaldaemon.com>, Jonathan Andrew says...Yeah that definately sucks. Why not be able to accept "args" much the same way main() can? i.e. myfunction(void[] args) { } Then pass it whatever you want, however you want. And then once foreach gets implemented you can ..... but I'm getting head of myself =).
The problem is, how do you call "myfunction"? Right now, you can use printf and sprintf very easily... just list out your parameters. In D currently, there is no way to initialize a non-static dynamic array. Perhaps if there was, you could use the syntax you describe above, for example: myfunction( new char*[] = ["parm1", "parm2"] ) Of course you can't vary the parameters types, it is quite ugly, and it requires heap allocation. So what else can be done? And on that topic, what is the right way to do printf style output and string formatting in D? Dan P.S.: being able to initialize non-static dynamic arrays like that would sure be nice.... P.P.S.: isn't it odd how probably the most basic and useful function among C's libraries uses such a hacked together and dangerous techniques?
Feb 27 2003
Kublai Kahn wrote:How is D going to handle variable length function arguments? It seems like Cs variable argument list is somewhat flawed and it doesn't support any built in handling of variable length arguments. It's almost hacked on with va_start and va_end and you have to include a library.
It's more than somewhat flawed; it's an artifact from the discredited B way of approaching function declarations. [Regulars have seen the following before.] The way I did this in DLI was to introduce the generic struct and a small addition to function declarations and calling: struct generic { TypeInfo type; void *data; } char [] fmt (char [] string, generic [] args...); You can pass a list of generics to a function with a consistent syntax: struct Marker { char [] fmt (char [] string, generic [] args...) { return toString () ~ ": " ~ format.fmt (string, args...); } } Extensions could allow specific types. Walter cried uncle on this subject a couple months ago, although I don't know what his plans are.
Feb 27 2003









"Achillefs Margaritis" <axilmar b-online.gr> 