Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 25, 2004 Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
How would I go about writing a function with a variable amount of parameters, a la printf, and could I use it in an interface to tell the derived classes that it had to implement a constructor with a variable amount of parameters? Cheers, Sigbjørn Lund Olsen |
February 25, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | Sigbjørn Lund Olsen wrote:
> How would I go about writing a function with a variable amount of parameters, a la printf, and could I use it in an interface to tell the derived classes that it had to implement a constructor with a variable amount of parameters?
>
> Cheers,
> Sigbjørn Lund Olsen
I believe that to write such a function, you need to do
extern (C):
and then implement it as you would in C.
The other stuff is probably not possible, though I don't really know.
Lars Ivar Igesund
|
February 25, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | Sigbjørn Lund Olsen wrote:
> How would I go about writing a function with a variable amount of parameters, a la printf, and could I use it in an interface to tell the derived classes that it had to implement a constructor with a variable amount of parameters?
D doesn't currently support defaults or the (terrifying) "..." signifier. I'm not sure there's a way to do what you're asking.
Sean
|
February 25, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote:
[...]
> variable amount of parameters
dmd supports the `...' qualifier and it is used in phobos.
Have a look at the source of `format' in std.string how to use it.
So long.
|
February 25, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | Manfred Nowak wrote:
> On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote:
>
> [...]
>
>>variable amount of parameters
>
> dmd supports the `...' qualifier and it is used in phobos.
Oops! Then I take back what I said.
Sean
|
March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | Damn, you still have to mess with that C crappy way. This way (C# style), it would be much better: void main() { printNumbers(1, 2, 3, 4, 5); } void printNumbers(params int[] list) { foreach(int number; list) { puts(number); } } The compiler can create that array for you. "params" will always have to be at the end of the parameters list, which is a clue for the compiler to create an array and put the the rest of the arguments in it. void main() { printStuff("Hello World!", 'D', 1, 2, 3, 4, 5); } void printStuff(char[] message, char letter, params int[] list) { puts(message); puts(letter); foreach(int number; list) { puts(number); } } On Wed, 25 Feb 2004 20:01:39 +0100, Manfred Nowak <svv1999@hotmail.com> wrote: > On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote: > > [...] >> variable amount of parameters > > dmd supports the `...' qualifier and it is used in phobos. > > Have a look at the source of `format' in std.string how to use it. > > So long. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SpookyET | We don't have a facility for compiling arrays at run-time. I was already arguing for that to be done like you propose, but it needs an array compiler. Even if we don't get it for varargs functions (which i think should be done), we could nontheless do:
printNumbers([1, 2, 3, 4, 5]);
which is not as pretty in this certain case, but is useful for many other things.
I was also wondering whether it is possible to syntactically unite it with tuples in a sane manner. Any ideas on it?
-eye
SpookyET wrote:
> Damn, you still have to mess with that C crappy way.
>
> This way (C# style), it would be much better:
>
> void main()
> {
> printNumbers(1, 2, 3, 4, 5);
> }
>
> void printNumbers(params int[] list)
> {
> foreach(int number; list)
> {
> puts(number);
> }
> }
>
> The compiler can create that array for you.
> "params" will always have to be at the end of the parameters list, which is a clue for the compiler to create an array and put the the rest of the arguments in it.
>
> void main()
> {
> printStuff("Hello World!", 'D', 1, 2, 3, 4, 5);
> }
>
> void printStuff(char[] message, char letter, params int[] list)
> {
> puts(message);
> puts(letter);
> foreach(int number; list)
> {
> puts(number);
> }
> }
>
> On Wed, 25 Feb 2004 20:01:39 +0100, Manfred Nowak <svv1999@hotmail.com> wrote:
>
>> On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote:
>>
>> [...]
>>
>>> variable amount of parameters
>>
>>
>> dmd supports the `...' qualifier and it is used in phobos.
>>
>> Have a look at the source of `format' in std.string how to use it.
>>
>> So long.
>
>
>
>
|
March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SpookyET | "SpookyET" <not4_u@hotmail.com> wrote in message news:opr36979x51s9n15@saturn... > Damn, you still have to mess with that C crappy way. > > This way (C# style), it would be much better: > > void main() > { > printNumbers(1, 2, 3, 4, 5); > } > > void printNumbers(params int[] list) > { > foreach(int number; list) > { > puts(number); > } > } A work-around for D would look something like: import std.c.stdio; extern (C) void test(int nargs,...) { // assumes the stack goes in a certain direction? int* args = cast(int*)(cast(va_list)&nargs+int.size); int[] arglist = args[0 .. nargs]; // now arglist is a "varargs" array. printf("last element is %d\n", arglist[arglist.length-1]); } int main(char[][] argv) { test(5, 1,2,3,4,5); return 0; } Not as nifty as C-sharp but still pretty usable. Also the D version doesn't allocate any memory - it is all on the stack If phobos would have some va_list sugar then the line int* args = cast(int*)(cast(va_list)&nargs+int.size); could be replaced with a call to phobos - maybe it could take care of the slicing, too. > The compiler can create that array for you. > "params" will always have to be at the end of the parameters list, which > is a clue for the compiler to create an array and put the the rest of the > arguments in it. > > void main() > { > printStuff("Hello World!", 'D', 1, 2, 3, 4, 5); > } > > void printStuff(char[] message, char letter, params int[] list) > { > puts(message); > puts(letter); > foreach(int number; list) > { > puts(number); > } > } > > On Wed, 25 Feb 2004 20:01:39 +0100, Manfred Nowak <svv1999@hotmail.com> wrote: > > > On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote: > > > > [...] > >> variable amount of parameters > > > > dmd supports the `...' qualifier and it is used in phobos. > > > > Have a look at the source of `format' in std.string how to use it. > > > > So long. > > > > -- > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | printNumbers([1, 2, 3, 4, 5]); Does't work On Mon, 01 Mar 2004 22:34:34 +0100, Ilya Minkov <minkov@cs.tum.edu> wrote: > We don't have a facility for compiling arrays at run-time. I was already arguing for that to be done like you propose, but it needs an array compiler. Even if we don't get it for varargs functions (which i think should be done), we could nontheless do: > > printNumbers([1, 2, 3, 4, 5]); > > which is not as pretty in this certain case, but is useful for many other things. > > I was also wondering whether it is possible to syntactically unite it with tuples in a sane manner. Any ideas on it? > > -eye > > > SpookyET wrote: > >> Damn, you still have to mess with that C crappy way. >> This way (C# style), it would be much better: >> void main() >> { >> printNumbers(1, 2, 3, 4, 5); >> } >> void printNumbers(params int[] list) >> { >> foreach(int number; list) >> { >> puts(number); >> } >> } >> The compiler can create that array for you. >> "params" will always have to be at the end of the parameters list, which is a clue for the compiler to create an array and put the the rest of the arguments in it. >> void main() >> { >> printStuff("Hello World!", 'D', 1, 2, 3, 4, 5); >> } >> void printStuff(char[] message, char letter, params int[] list) >> { >> puts(message); >> puts(letter); >> foreach(int number; list) >> { >> puts(number); >> } >> } >> On Wed, 25 Feb 2004 20:01:39 +0100, Manfred Nowak <svv1999@hotmail.com> wrote: >> >>> On Wed, 25 Feb 2004 17:48:26 +0100, Sigbjørn Lund Olsen wrote: >>> >>> [...] >>> >>>> variable amount of parameters >>> >>> >>> dmd supports the `...' qualifier and it is used in phobos. >>> >>> Have a look at the source of `format' in std.string how to use it. >>> >>> So long. >> -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
March 01, 2004 Re: Variable numbers of function parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to SpookyET | In article <opr37gm5ti1s9n15@saturn>, SpookyET says...
>
>printNumbers([1, 2, 3, 4, 5]); Does't work
>
>--
>Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
No, D doesn't let you create arrays on the fly (that's the way it's said,
right?).
-------------------
Carlos Santander B.
|
Copyright © 1999-2021 by the D Language Foundation