Skip to content

Commit 86b49e1

Browse files
authored
[function.dd] Explain why a Lazy Variadic Delegate is better than a Lazy Variadic Array
1 parent 051150a commit 86b49e1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spec/function.dd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,25 @@ foo(1, 3+x, dg, cast(int delegate())null);
17151715

17161716
---
17171717
foo( { return 1; }, { return 3+x; }, dg, null );
1718+
---
1719+
1720+
$(P The lazy variadic delegate solution is preferable to using a lazy
1721+
variadic array, because each array index would evaluate every element:)
1722+
1723+
---
1724+
import std.stdio;
1725+
1726+
void foo(lazy int[] arr...)
1727+
{
1728+
writeln(arr[0]); // 1
1729+
writeln(arr[1]); // 4, not 2
1730+
}
1731+
1732+
void main()
1733+
{
1734+
int x;
1735+
foo(++x, ++x);
1736+
}
17181737
---
17191738

17201739
$(H2 $(LEGACY_LNAME2 Local Variables, local-variables, Local Variables))

0 commit comments

Comments
 (0)