Skip to content

Commit ce6d37e

Browse files
authored
[spec] Improve value sequence docs (#3715)
* [spec] Improve value sequence docs Prefer using TypeSeq for variadic template functions. Mention returning Tuple. Add subheading and example for lvalue sequences. Minor tweaks. * Remove note
1 parent ce81035 commit ce6d37e

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

spec/template.dd

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,42 @@ $(H4 $(LNAME2 homogeneous_sequences, Homogeneous Sequences))
933933
---
934934
)
935935

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+
936941
$(P A *TypeSeq* can similarly be used to
937942
$(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
939944
*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+
940972
$(UL
941973
$(LI `.tupleof` can be $(DDSUBLINK spec/class, class_properties, used on a class)
942974
or struct instance to obtain an lvalue sequence of its fields.)
@@ -952,7 +984,8 @@ $(H4 $(LNAME2 seq-ops, Sequence Operations))
952984
$(LI The $(I n)th element can be retrieved by
953985
$(DDSUBLINK spec/expression, index_operations, indexing) an
954986
$(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.)
956989
$(LI $(DDSUBLINK spec/expression, slice_operations, Slicing)
957990
produces a new sequence with a subset of the elements of the original sequence.)
958991
)
@@ -962,12 +995,13 @@ $(H4 $(LNAME2 seq-ops, Sequence Operations))
962995
import std.meta : AliasSeq;
963996

964997
int v = 4;
998+
// alias a sequence of 3 values and one symbol
965999
alias nums = AliasSeq!(1, 2, 3, v);
9661000
static assert(nums.length == 4);
9671001
static assert(nums[1] == 2);
9681002

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
9711005
assert(v == 5);
9721006

9731007
// slice first 3 elements

0 commit comments

Comments
 (0)