@@ -933,10 +933,42 @@ $(H4 $(LNAME2 homogeneous_sequences, Homogeneous Sequences))
933
933
---
934
934
)
935
935
936
+ $(NOTE A value sequence cannot be returned from a function - instead, return a
937
+ $(REF Tuple, std,typecons).)
938
+
939
+ $(H4 $(LNAME2 lvalue-sequences, Lvalue Sequences))
940
+
936
941
$(P A *TypeSeq* can similarly be used to
937
942
$(DDSUBLINK articles/ctarguments, type-seq-instantiation, declare variables).
938
- Parameters or variables declared with a *TypeSeq* are called an
943
+ Parameters or variables whose type is a *TypeSeq* are called an
939
944
*lvalue sequence*.)
945
+
946
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
947
+ ---
948
+ void main()
949
+ {
950
+ import std.meta: AliasSeq;
951
+
952
+ // use a type alias just for convenience
953
+ alias TS = AliasSeq!(string, int);
954
+ TS tup; // lvalue sequence
955
+ assert(tup == AliasSeq!("", 0)); // TS.init
956
+
957
+ int i = 5;
958
+ // initialize another lvalue sequence from a sequence of a value and a symbol
959
+ auto tup2 = AliasSeq!("hi", i); // value of i is copied
960
+ i++;
961
+ enum hi5 = AliasSeq!("hi", 5); // rvalue sequence
962
+ static assert(is(typeof(hi5) == TS));
963
+ assert(tup2 == hi5);
964
+
965
+ // lvalue sequence elements can be modified
966
+ tup = tup2;
967
+ assert(tup == hi5);
968
+ }
969
+ ---
970
+ )
971
+
940
972
$(UL
941
973
$(LI `.tupleof` can be $(DDSUBLINK spec/class, class_properties, used on a class)
942
974
or struct instance to obtain an lvalue sequence of its fields.)
@@ -952,7 +984,8 @@ $(H4 $(LNAME2 seq-ops, Sequence Operations))
952
984
$(LI The $(I n)th element can be retrieved by
953
985
$(DDSUBLINK spec/expression, index_operations, indexing) an
954
986
$(I AliasSeq) with `Seq[n]`. Indexes must be known at compile-time.
955
- The result is an lvalue when the element is a variable.)
987
+ The result is an lvalue when the element is a symbol which resolves to a variable,
988
+ or when the sequence is an lvalue sequence.)
956
989
$(LI $(DDSUBLINK spec/expression, slice_operations, Slicing)
957
990
produces a new sequence with a subset of the elements of the original sequence.)
958
991
)
@@ -962,12 +995,13 @@ $(H4 $(LNAME2 seq-ops, Sequence Operations))
962
995
import std.meta : AliasSeq;
963
996
964
997
int v = 4;
998
+ // alias a sequence of 3 values and one symbol
965
999
alias nums = AliasSeq!(1, 2, 3, v);
966
1000
static assert(nums.length == 4);
967
1001
static assert(nums[1] == 2);
968
1002
969
- // nums[3] is bound to v, an lvalue
970
- nums[3]++;
1003
+ //nums[0]++; // Error, nums[0] is an rvalue
1004
+ nums[3]++; // OK, nums[3] is bound to v, an lvalue
971
1005
assert(v == 5);
972
1006
973
1007
// slice first 3 elements
0 commit comments