@@ -973,41 +973,42 @@ $(H4 $(LNAME2 front-seq, Multiple Element Values))
973
973
expands to a $(DDSUBLINK spec/template, homogeneous_sequences, value sequence)
974
974
whose length matches the number of variables. Each variable is assigned
975
975
to the corresponding value in the sequence.
976
+ If there is only one variable, it will be a value sequence.
976
977
)
977
978
978
979
$(SPEC_RUNNABLE_EXAMPLE_RUN
979
980
---
981
+ import std.stdio;
982
+
983
+ // simple version of std.typecons.Tuple
980
984
struct Tuple(Types...) // takes a TypeSeq
981
985
{
982
986
Types items; // ValueSeq
983
987
alias items this; // decay to a value sequence
984
988
}
985
989
986
- // Infinite range with a repeating element, which is a tuple
990
+ string s = "hello";
991
+
987
992
struct TupleRange
988
993
{
989
- enum front = Tuple!(char, bool, int)('a', true, 2) ;
990
- enum bool empty = false ;
991
-
992
- void popFront() {}
994
+ size_t i ;
995
+ auto front() => Tuple!(size_t, char)(i, s[i]) ;
996
+ bool empty() => i >= s.length;
997
+ void popFront() { i++; }
993
998
}
994
999
995
1000
void main()
996
1001
{
997
1002
// Tuple destructuring
998
- foreach (a, b , c; TupleRange())
1003
+ foreach (i , c; TupleRange())
999
1004
{
1000
- assert(a == 'a');
1001
- assert(b == true);
1002
- assert(c == 2);
1003
- break;
1005
+ writeln(i, ": ", c);
1004
1006
}
1007
+ writeln();
1005
1008
// Tuple variable
1006
1009
foreach (tup; TupleRange())
1007
1010
{
1008
- assert(tup[0] == 'a');
1009
- assert(tup == TupleRange.front);
1010
- break;
1011
+ writefln("(%s, %s)", tup[0], tup[1]);
1011
1012
}
1012
1013
}
1013
1014
---
0 commit comments