Thread overview
What is the corect behavour for lazy in foreach variadic template
Sep 25, 2015
Sean Campbell
Sep 25, 2015
Ali Çehreli
Sep 25, 2015
Artur Skawina
Sep 25, 2015
Ali Çehreli
September 25, 2015
Take the following code for example

module test;
import std.stdio;

void test(T...)(lazy T args)
{
	foreach(arg;args) //bar is invoked here
	{
		writeln("about to process arg");
		writefln("processing arg %s",arg); //but it should be invoked here, right?
	}
}

int bar()
{
	writeln("bar invoked");
	return 1;
}

void main()
{
	test(bar());
}

shouldn't bar be evaluated when writefln is called, not at the start of the loop.
September 25, 2015
On 09/25/2015 05:56 AM, Sean Campbell wrote:
> Take the following code for example
>
> module test;
> import std.stdio;
>
> void test(T...)(lazy T args)

I don't think that syntax is implemented. It looks valid to me though. There are many qualifier combinations that the compiler is silent about. I think this is one of those cases.

Are you familiar with the 'Lazy variadic functions' feature? It works but as far as I understand it, all arguments must be of the same type:

  http://dlang.org/function.html

As long as all 'int's are acceptable, the change to your code would be

  void test(int delegate()[] args...)

There is the following related discussion:

  http://forum.dlang.org/post/ppcnyjpzcfptkoxkdrtk@forum.dlang.org

I haven't tried it from but John Colvin's solution there probably has the same issue.

Perhaps we need an enhancement that either works in your original code or lazy variadics to support something like the following:

  void test(T...)(T delegate()[] args...)

Ali

September 25, 2015
On 09/25/15 17:47, Ali Çehreli via Digitalmars-d-learn wrote:

> Perhaps we need an enhancement that either works in your original code [...]

His original code does work (`foreach` evaluates `args[N]` and
assigns the result to `arg`).

If he wanted to delay the evaluation, he would have written it like this:

   void test(T...)(lazy T args)
   {
       foreach(I, _; typeof(args))
       {
           writeln("about to process arg");
           writefln("processing arg %s",args[I]);
       }
   }

artur
September 25, 2015
On 09/25/2015 09:38 AM, Artur Skawina via Digitalmars-d-learn wrote:
> On 09/25/15 17:47, Ali Çehreli via Digitalmars-d-learn wrote:
>
>> Perhaps we need an enhancement that either works in your original code [...]
>
> His original code does work (`foreach` evaluates `args[N]` and
> assigns the result to `arg`).
>
> If he wanted to delay the evaluation, he would have written it
> like this:
>
>     void test(T...)(lazy T args)
>     {
>         foreach(I, _; typeof(args))
>         {
>             writeln("about to process arg");
>             writefln("processing arg %s",args[I]);
>         }
>     }
>
> artur
>

Awesome! :)

Ali