Jump to page: 1 2
Thread overview
Variadic functions: How to pass another variadic function the variadic args?
Aug 03, 2013
Gabi
Aug 03, 2013
bearophile
Aug 03, 2013
Ali Çehreli
Aug 03, 2013
monarch_dodra
Aug 03, 2013
bearophile
Aug 04, 2013
Ali Çehreli
Aug 04, 2013
monarch_dodra
Aug 03, 2013
Gabi
Aug 03, 2013
monarch_dodra
Aug 03, 2013
Gabi
Aug 03, 2013
bearophile
Aug 03, 2013
monarch_dodra
August 03, 2013
void F1(...)
{

}

void F2(...)
{
  //HOW TO pass F1(..) the args we were called with ?

}
August 03, 2013
Gabi:

>   //HOW TO pass F1(..) the args we were called with ?


import std.stdio;

void f1(Args...)(Args args) {
    foreach (arg; args)
        arg.writeln;
}

void f2(Args...)(Args args) {
    f1(args);
}

void main() {
    f2(10, "hello", 1.5);
}


Bye,
bearophile
August 03, 2013
On 08/03/2013 07:58 AM, bearophile wrote:

> Gabi:
>
>>   //HOW TO pass F1(..) the args we were called with ?
>
>
> import std.stdio;
>
> void f1(Args...)(Args args) {
>      foreach (arg; args)
>          arg.writeln;

Would you expect the following two lines behave the same?

    writeln(args);
    writefln("%s", args);

Apparently not:

10hello1.5
10

Why?

> }
>
> void f2(Args...)(Args args) {
>      f1(args);
> }
>
> void main() {
>      f2(10, "hello", 1.5);
> }
>
>
> Bye,
> bearophile

Ali

August 03, 2013
On Saturday, 3 August 2013 at 15:10:20 UTC, Ali Çehreli wrote:
> On 08/03/2013 07:58 AM, bearophile wrote:
>
> > Gabi:
> >
> >>   //HOW TO pass F1(..) the args we were called with ?
> >
> >
> > import std.stdio;
> >
> > void f1(Args...)(Args args) {
> >      foreach (arg; args)
> >          arg.writeln;
>
> Would you expect the following two lines behave the same?
>
>     writeln(args);
>     writefln("%s", args);

I wouldn't.

> Apparently not:
>
> 10hello1.5
> 10
>
> Why?

writeln simply prints all the args it receives, then a line break.

writefln, on the other end, only prints its single "fmt" arg. The rest of the args are only used as they are referenced in fmt. Your code basically boils down to:

writefln("%s", 10, "hello", 1.5);
=> 10
August 03, 2013
monarch_dodra:

> writefln("%s", 10, "hello", 1.5);
> => 10

According to a recent discussion with Andrei, that's (thankfully) going to become a run-time exception:

http://d.puremagic.com/issues/show_bug.cgi?id=4927

Bye,
bearophile
August 03, 2013
On Saturday, 3 August 2013 at 14:58:49 UTC, bearophile wrote:
> Gabi:
>
>>  //HOW TO pass F1(..) the args we were called with ?
>
>
> import std.stdio;
>
> void f1(Args...)(Args args) {
>     foreach (arg; args)
>         arg.writeln;
> }
>
> void f2(Args...)(Args args) {
>     f1(args);
> }
>
> void main() {
>     f2(10, "hello", 1.5);
> }
>
>
> Bye,
> bearophile

Thanks but how do I do this for variadic functions (using _arguments and friends) ? Not variadic templates..
More specifically I want to know at runtime the type of each parameter
August 03, 2013
On Saturday, 3 August 2013 at 16:57:41 UTC, Gabi wrote:
> On Saturday, 3 August 2013 at 14:58:49 UTC, bearophile wrote:
>> Gabi:
>>
>>> //HOW TO pass F1(..) the args we were called with ?
>>
>>
>> import std.stdio;
>>
>> void f1(Args...)(Args args) {
>>    foreach (arg; args)
>>        arg.writeln;
>> }
>>
>> void f2(Args...)(Args args) {
>>    f1(args);
>> }
>>
>> void main() {
>>    f2(10, "hello", 1.5);
>> }
>>
>>
>> Bye,
>> bearophile
>
> Thanks but how do I do this for variadic functions (using _arguments and friends) ? Not variadic templates..
> More specifically I want to know at runtime the type of each parameter

I don't know how to do runtime variadics in D, and quite frankly, I don't want to know: They are an abomination, and I don't want to be anywhere near these things.

My guess though, is that it's the same syntax as in C? Use a straight up elispis:

void foo(...).

Note that you *can't* extract the types from the vararg unless you *guess* them from an alternative source (for example, "fmt" in the printf function)

Also, importing "core.vararg" should get you whatever you'd get in 'vararg.h'/"stdarg.h". From there, I don't think D does anything specific that's not actually just C.
August 03, 2013
On Saturday, 3 August 2013 at 18:48:17 UTC, monarch_dodra wrote:
> On Saturday, 3 August 2013 at 16:57:41 UTC, Gabi wrote:
>> On Saturday, 3 August 2013 at 14:58:49 UTC, bearophile wrote:
>>> Gabi:
>>>
>>>> //HOW TO pass F1(..) the args we were called with ?
>>>
>>>
>>> import std.stdio;
>>>
>>> void f1(Args...)(Args args) {
>>>   foreach (arg; args)
>>>       arg.writeln;
>>> }
>>>
>>> void f2(Args...)(Args args) {
>>>   f1(args);
>>> }
>>>
>>> void main() {
>>>   f2(10, "hello", 1.5);
>>> }
>>>
>>>
>>> Bye,
>>> bearophile
>>
>> Thanks but how do I do this for variadic functions (using _arguments and friends) ? Not variadic templates..
>> More specifically I want to know at runtime the type of each parameter
>
> I don't know how to do runtime variadics in D, and quite frankly, I don't want to know: They are an abomination, and I don't want to be anywhere near these things.
>
> My guess though, is that it's the same syntax as in C? Use a straight up elispis:
>
> void foo(...).
>
> Note that you *can't* extract the types from the vararg unless you *guess* them from an alternative source (for example, "fmt" in the printf function)
>
> Also, importing "core.vararg" should get you whatever you'd get in 'vararg.h'/"stdarg.h". From there, I don't think D does anything specific that's not actually just C.

Ok, I got an alternative solution. What do you think about the following ?

    void f(T:long) (T arg)
    {
      ...
    }
    void f(T:int) (T arg)
    {
         ...
    }

    void f(T:int) (T arg)
    {
         ...
    }
    void f(T:string) (T arg)
    {
         ...
    }

    void f1(ARGS...)(ARGS args)
    {
       foreach(arg; args)
       {
         f(arg);
       }
    }



August 03, 2013
monarch_dodra:

> My guess though, is that it's the same syntax as in C? Use a straight up elispis:
>
> void foo(...).
>
> Note that you *can't* extract the types from the vararg unless you *guess* them from an alternative source (for example, "fmt" in the printf function)
>
> Also, importing "core.vararg" should get you whatever you'd get in 'vararg.h'/"stdarg.h". From there, I don't think D does anything specific that's not actually just C.

D supports both C and D style variadiac functions. D variadiac functions also have a _arguments of type TypeInfo[].

See here about in the middle of the page:
http://dlang.org/function

(This is why we ask people like JS "why do you need that?", because very often there are better solutions if you know the real problem to solve).

Bye,
bearophile
August 03, 2013
On Saturday, 3 August 2013 at 19:12:40 UTC, bearophile wrote:
> monarch_dodra:
>
>> My guess though, is that it's the same syntax as in C? Use a straight up elispis:
>>
>> void foo(...).
>>
>> Note that you *can't* extract the types from the vararg unless you *guess* them from an alternative source (for example, "fmt" in the printf function)
>>
>> Also, importing "core.vararg" should get you whatever you'd get in 'vararg.h'/"stdarg.h". From there, I don't think D does anything specific that's not actually just C.
>
> D supports both C and D style variadiac functions. D variadiac functions also have a _arguments of type TypeInfo[].
>
> See here about in the middle of the page:
> http://dlang.org/function

The more you know I guess. Good to see D made the whole thing more robust. I've never had a usecase for runtime variadic, but thanks for the link.

« First   ‹ Prev
1 2