Skip to content

Commit 8f6609e

Browse files
authored
Merge pull request #3001 from MoonlightSentinel/tuple-foreach
statement.dd: Add example for tuple expansion in foreach
2 parents 8b841c1 + fca87cb commit 8f6609e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

spec/statement.dd

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,43 @@ $(H3 $(LEGACY_LNAME2 foreach_with_ranges, foreach-with-ranges, Foreach over Stru
815815
}
816816
---
817817

818+
$(P Multiple loop variables are allowed if the `front` property returns a type that
819+
expands to an $(DDSUBLINK spec/template, variadic-templates, expression sequence)
820+
whose size matches the number of variables. Each variable is assigned
821+
to the corresponding value in the tuple.
822+
)
823+
824+
---
825+
// Common tuple implementation that can decay into its members
826+
import std.typecons : Tuple;
827+
828+
// Range whose elements are tuples
829+
struct TupleRange
830+
{
831+
Tuple!(char, bool, int) front()
832+
{
833+
return typeof(return)('a', true, 2);
834+
}
835+
836+
bool empty() { return false; }
837+
838+
void popFront() {}
839+
}
840+
841+
void main()
842+
{
843+
foreach (a, b, c; TupleRange())
844+
{
845+
assert(a == 'a');
846+
assert(b == true);
847+
assert(c == 2);
848+
}
849+
850+
// Expected 3 arguments, not 1
851+
// foreach (a; TupleRange()) { ... }
852+
}
853+
---
854+
818855
$(H3 $(LNAME2 foreach_over_delegates, Foreach over Delegates))
819856

820857
$(P If $(I ForeachAggregate) is a delegate, the type signature of

0 commit comments

Comments
 (0)