On 8/1/18 10:14 PM, Shachar Shemesh wrote:
On 01/08/18 17:13, Steven Schveighoffer wrote:
The lazy variadic thing is a distinction between specifying variadic
lazy parameters and a lazy variadic array.
I have now read that sentence 4 times, and I still have no idea what it
means.
Can you give examples of both?
import std.stdio;
// lazy variadic array
void foo(lazy int[] arr...)
{
writeln(arr[0]);
writeln(arr[1]);
writeln(arr[2]);
}
// variadic lazy paramters
void bar(int delegate()[] items...)
{
writeln(items[0]());
writeln(items[1]());
writeln(items[2]());
}
int param(int x)
{
writeln("param ", x);
return x;
}
void main()
{
foo(param(0), param(1), param(2));
bar(param(0), param(1), param(2));
}
output:
param 0
param 1
param 2
0
param 0
param 1
param 2
1
param 0
param 1
param 2
2
param 0
0
param 1
1
param 2
2
So in the first case, the ENTIRE array is evaluated lazily, and then an
element selected. In the second case, each item is evaluated when used.
-Steve