What is the corect behavour for lazy in foreach variadic template

Sean Campbell via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 25 05:56:08 PDT 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.


More information about the Digitalmars-d-learn mailing list