Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 04, 2001 variable number of arguments | ||||
---|---|---|---|---|
| ||||
Is there support for variable number of arguments in D? Something like VB's ParamArray, or maybe another (better?) way... or is this something that's not going to be the language feature? |
October 05, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to EvilOne | EvilOne wrote:
>
> Is there support for variable number of arguments in D? Something like VB's ParamArray, or maybe another (better?) way... or is this something that's not going to be the language feature?
printf is supported. I would guess that something like C's varargs will be supported. It's a shame that useful (in my mind) features can be dropped because that are dangerous, but we keep C's varargs (and printf for that matter). (I guess this is assuming that varargs must be supported for printf) Doesn't var-args rely on macros?
Dan
|
October 05, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to a |
a wrote in message <3BBD3952.29C9B2A4@b.c>...
>EvilOne wrote:
>> Is there support for variable number of arguments in D? Something like VB's ParamArray, or maybe another (better?) way... or is this something that's not going to be the language feature?
>printf is supported. I would guess that something like C's varargs will be supported. It's a shame that useful (in my mind) features can be dropped because that are dangerous, but we keep C's varargs (and printf for that matter). (I guess this is assuming that varargs must be supported for printf) Doesn't var-args rely on macros?
D will support variable arguments, but only in interfacing to C functions. It's only real use is for printf(), etc.
|
October 07, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Hello In a general way, couldn't it be solved like this: void printf("format string", *void[]); That is an array with pointers(hope i got the syntax right). And since array lenghts are a property of array this would be an easy thing to implement. A obvious problem though, is how would you know that the thing pointed to at place n is the same type as entity n in the format string, but from what I can see that is a problem with var args to. /Pontus |
October 07, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to pontus | The problem is that arguments on the stack can be different sizes. pontus wrote in message <3BC071D5.D574F0AC@student.uu.se>... Hello In a general way, couldn't it be solved like this: void printf("format string", *void[]); That is an array with pointers(hope i got the syntax right). And since array lenghts are a property of array this would be an easy thing to implement. A obvious problem though, is how would you know that the thing pointed to at place n is the same type as entity n in the format string, but from what I can see that is a problem with var args to. /Pontus |
October 08, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter |
Well,maybe i'm missing something, but with a string and an array of pointers, what on the stack have diffrent size?
Walter wrote:
>
> The problem is that arguments on the stack can be different sizes.
>
> pontus wrote in message <3BC071D5.D574F0AC@student.uu.se>...
>> Hello
>>
>> In a general way, couldn't it be solved like this:
>>
>> void printf("format string", *void[]);
>>
>> That is an array with pointers(hope i got the syntax right). And since array
>> lenghts are a property of array this would be an easy thing to implement.
>> A obvious problem though, is how would you know that the thing pointed to at
>> place n is the same type as entity n in the format string, but from what I
>> can see that is a problem with var args to.
>>
>> /Pontus
|
October 08, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pontus Pihlgren | Pontus Pihlgren wrote: void myfunc(char a, char b, char); will possibly put something like this on stack: +--+--+--+ | a | b | c | +--+--+--+ where a, b and c is only one byte long. Today on almost all 32bit processesor (void *) ist 4 byte long so *void[3] will look like this +-----------+-----------+-----------+ | *void[0] | *void[1] | *void[2] | +-----------+-----------+-----------+ each element is 4 bytes wide, see the difference? and why (void *) can most times not be used to track function arguments. It is only possible if the compiler always puts 4 bytes on stack for every argument even smaller ones, and of course also never bigger ones like 'long long', but this might be profuseness, however honestly I've no idea how compilers do today... or how printf("%c%c%c", a, b, c); will be placed on stack for an x86 processsor, maybe it uses 4 byte frames after all, in which case a (void **) could actually function... |
October 08, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Axel Kittenberger | Given: printf(" ... ", char, int, long long, double, long double); the compiler pushes on the stack: char 4 int 4 long long 8 double 8 ld 12 and in general any struct can be passed by value, and can be any (aligned) size. Every scheme I've come up with to deal with this is inefficient. |
October 09, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | What were your (Walter's) thoughts about the recursive scheme I cooked up? I remember other people commented on it, but I don't remember you speaking about it. D code: char[] printf(char[],....) // declares that variable argument lists are ok, no body may be defined out(result) { assert(result == ""); }; char[] printf(char[],char) {...}; char[] printf(char[],int) {...}; char[] printf(char[],char[]) {...}; char[] printf(char[],void*) {...}; Each version of printf accepts as its first argument the format string. It will throw an exception if the format specifier is invalid for the type. Otherwise, it does the printing and returns any remaining portion of the format string. The compiler expands the variable argument call inline: D call: char c; char[] s; int i; printf("%c %s %d",c,s,i); Expands to: char c; char[] s; int i; printf(printf(printf("%c %s %d",c),s),i); None of which are variable argument fucntion calls, and all of which are typesafe. The innermost printf returns the string "%s %d", while the next returns "%d", while the last returns "". Only then is the out condition checked; it is fine, so the program goes on. Note that this requires that the 1st argument of the call be the same type as the return type; anything else would be a syntax error in this scheme. To allow multiple fixed arguments, the format is similar: int foo(int, // recursive argument char,void*,char[], // these arguments are all called the same to all copies of the recursion. ....); // variable argument. -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
October 09, 2001 Re: variable number of arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3BC2A336.74A74409@deming-os.org... > What were your (Walter's) thoughts about the recursive scheme I cooked up? I remember other people commented on it, but I don't remember you speaking about it. [...] It would be even better if there's a standartized format string, so it could be generated by D compiler itself by just looking at argument list. So, you can type: char c; char[] s; printf(5, c, s); // format string will _automatically_ be "%d %c %s" |
Copyright © 1999-2021 by the D Language Foundation