Jump to page: 1 2
Thread overview
Variable numbers of function parameters?
Feb 25, 2004
Lars Ivar Igesund
Feb 25, 2004
Sean Kelly
Feb 25, 2004
Manfred Nowak
Feb 25, 2004
Sean Kelly
Mar 01, 2004
SpookyET
Mar 01, 2004
Ilya Minkov
Mar 01, 2004
SpookyET
Mar 02, 2004
Derek Parnell
Mar 01, 2004
Ben Hinkle
Mar 02, 2004
Ben Hinkle
Mar 02, 2004
Ilya Minkov
Mar 02, 2004
Ben Hinkle
Mar 02, 2004
Matthias Becker
February 25, 2004
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
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
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
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
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
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
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
"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
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
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.
« First   ‹ Prev
1 2