Skip to content

Commit 496dd44

Browse files
authored
Merge pull request #2204 from ntrel/ct-seq
Improve Compile-time sequences docs
2 parents 2f2c237 + 68c2586 commit 496dd44

File tree

4 files changed

+98
-52
lines changed

4 files changed

+98
-52
lines changed

articles/ctarguments.dd

Lines changed: 93 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
Ddoc
22

3-
$(D_S Compile-time Argument Lists,
3+
$(D_S $(TITLE),
44

55
$(HEADERNAV_TOC)
66

7-
$(P Compile-time lists is an important metaprogramming concept that comes naturally
7+
$(H2 $(LNAME2 Background, Background))
8+
9+
$(P Compile-time sequences are an important metaprogramming concept that comes naturally
810
from D support for $(LINK2 variadic-function-templates.html, variadic templates). They
9-
allow a programmer to operate on types, symbols and expressions enabling the ability to define
10-
compile-time algorithms that operate on types, symbols and expressions.
11+
allow a programmer to operate on types, symbols and values, enabling the ability to define
12+
compile-time algorithms that operate on types, symbols and values.
1113
)
1214

13-
$(P For historical reasons those sometimes can be called tuples in documentation or compiler
14-
internals but don't get confused : this doesn't have much in common with tuples that
15+
$(P $(B Note:) For historical reasons these sequences can sometimes be called tuples in documentation or compiler
16+
internals, but don't get confused - they don't have much in common with tuples that
1517
commonly exist in other languages. Sequences of values of different types that can be
16-
returned from functions are provided by $(LINK2 phobos/std_typecons.html#.Tuple, std.typecons.Tuple).
17-
Using term "tuple" to mean compile-time lists is discouraged to avoid confusion, and if encountered
18+
returned from functions are provided by $(REF Tuple, std, typecons).
19+
Using the term "tuple" to mean compile-time sequences is discouraged to avoid confusion, and if encountered
1820
should result in a $(LINK2 https://issues.dlang.org, documentation bug report).
1921
)
2022

21-
$(P Consider this simple snippet:)
23+
$(P Consider this simple variadic template:)
2224

2325
---
2426
template Variadic(T...) { /* ... */ }
2527
---
2628

27-
$(P `T` here is variadic $(LINK2 spec/template.html#TemplateArgumentList, template argument list)
29+
$(P `T` here is a $(DDSUBLINK spec/template, variadic-templates, TemplateParameterSequence),
2830
which is a core language feature. It has its own special semantics,
2931
and, from the programmer's point of view, is most similar to an array of compile-time entities - types,
30-
symbols (names) and expressions (values). One can check the length of this list
32+
symbols (names) and values. One can check the length of this sequence
3133
and access any individual element:
3234
)
3335

@@ -39,34 +41,36 @@ $(HEADERNAV_TOC)
3941
pragma(msg, T[1]);
4042
}
4143

42-
alias Dummy = Variadic!(int, 42);
44+
alias dummy = Variadic!(int, 42);
4345
// prints during compilation:
4446
// int
4547
// 42
4648
---
4749

48-
$(P However, the language itself does not provide any means to define such lists outside of
50+
$(H2 $(LNAME2 AliasSeq, AliasSeq))
51+
52+
$(P The language itself does not provide any means to define such sequences outside of
4953
a template parameter declaration. Instead, there is a
50-
$(MREF_ALTTEXT simple utility, std, meta) provided by the D standard
54+
$(MREF_ALTTEXT simple utility, std, meta) provided by the standard
5155
library:
5256
)
5357

5458
---
5559
alias AliasSeq(T...) = T;
5660
---
5761

58-
$(P All it does is give a specific variadic argument list an externally accessible name so
62+
$(P All it does is give a specific variadic argument sequence an externally accessible name so
5963
that it can be worked with in any other context:
6064
)
6165

6266
---
6367
import std.meta;
6468
// can alias to some other name
6569
alias Name = AliasSeq!(int, 42);
66-
pragma(msg, Name[0]);
67-
pragma(msg, Name[1]);
68-
// or work with a list directly
69-
pragma(msg, AliasSeq!("aaa", 0, double)[2]);
70+
pragma(msg, Name[0]); // int
71+
pragma(msg, Name[1]); // 42
72+
// or work with a sequence directly
73+
pragma(msg, AliasSeq!("aaa", 0, double)[2]); // double
7074
---
7175

7276
$(H2 $(LNAME2 available-operations, Available operations))
@@ -92,27 +96,27 @@ $(H2 $(LNAME2 available-operations, Available operations))
9296

9397
$(H3 $(LNAME2 assignment, Assignment))
9498

95-
$(P Works only if the list element is a symbol that refers to a mutable variable)
99+
$(P Works only if the sequence element is a symbol that refers to a mutable variable)
96100

97101
---
98102
import std.meta;
99103

100104
void main()
101105
{
102106
int x;
103-
alias List = AliasSeq!(10, x);
104-
List[1] = 42;
107+
alias seq = AliasSeq!(10, x);
108+
seq[1] = 42;
105109
assert (x == 42);
106-
// List[0] = 42; // won't compile, can't assign to a constant
110+
// seq[0] = 42; // won't compile, can't assign to a constant
107111
}
108112
---
109113

110114
$(H3 $(LNAME2 loops, Loops))
111115

112116
$(P D's $(LINK2 spec/statement.html#ForeachStatement, foreach statement) has special
113-
semantics when iterating over compile-time lists. It repeats the body of the loop
114-
for each of the list elements, with the loop iteration "variable" becoming an alias
115-
for each compile-time list element in turn.
117+
semantics when iterating over compile-time sequences. It repeats the body of the loop
118+
for each of the sequence elements, with the loop iteration symbol being an alias
119+
for each compile-time sequence element in turn.
116120
)
117121

118122
---
@@ -130,17 +134,19 @@ $(H2 $(LNAME2 available-operations, Available operations))
130134
}
131135

132136
/* Prints:
133-
134137
int
135138
string
136139
void()
137140
*/
138141
---
139142

143+
$(P $(B Note:) $(DDSUBLINK version, staticforeach, Static foreach)
144+
should be preferred in new code.)
145+
140146
$(H2 $(LNAME2 auto-expansion, Auto-expansion))
141147

142-
$(P One less obvious property of compile-time argument lists is that when used
143-
as an argument to a function or template, they are automatically treated as a list
148+
$(P One less obvious property of compile-time argument sequences is that when used
149+
as an argument to a function or template, they are automatically treated as a sequence
144150
of comma-separated arguments:
145151
)
146152

@@ -158,25 +164,29 @@ $(H2 $(LNAME2 auto-expansion, Auto-expansion))
158164
$(P This will only print `int` during compilation because the last line gets rewritten
159165
as `alias Dummy = Print0!(int, double)`. If auto-expansion didn't happen,
160166
`AliasSeq!(int, double)` would be printed instead. This is an inherent part of
161-
the language semantics for variadic lists, and thus also preserved by `AliasSeq`.
167+
the language semantics for variadic sequences, and thus also preserved by `AliasSeq`.
162168
)
163169

164-
$(H2 $(LNAME2 homogenous-lists, Homogenous lists))
170+
$(H2 $(LEGACY_LNAME2 homogenous-lists, homogenous-sequences, Homogenous sequences))
165171

166-
$(P An `AliasSeq` that consist of only type or expression (value) elements are
167-
commonly called "type lists" or "expression lists" respectively. The concept of
168-
a "symbol list" is rarely mentioned explicitly but fits the same pattern.
172+
$(P An $(ALOCAL AliasSeq, AliasSeq) that consists of only type or value elements are
173+
commonly called "type sequences" or "value sequences" respectively. The concept of
174+
a "symbol sequence" is used less frequently but fits the same pattern.
169175
)
170176

171-
$(P It is possible to use homogenous type lists in declarations:)
177+
$(H3 $(LNAME2 type-seq, Type sequences))
178+
179+
$(P It is possible to use homogenous type sequences in declarations:)
172180

173181
---
174182
import std.meta;
175183
alias Params = AliasSeq!(int, double, string);
176184
void foo(Params); // void foo(int, double, string);
177185
---
178186

179-
$(P D supports a special variable declaration syntax where a type list acts as a type:)
187+
$(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
188+
189+
$(P D supports a special variable declaration syntax where a type sequence acts as a type:)
180190

181191
---
182192
import std.meta;
@@ -188,35 +198,72 @@ $(H2 $(LNAME2 homogenous-lists, Homogenous lists))
188198
variables[1] = 42.0;
189199
variables[2] = "just a normal variable";
190200
}
191-
192-
/* The compiler will rewrite such a declaration to something like this:
193-
201+
---
202+
$(P The compiler will rewrite such a declaration to something like this:)
203+
---
194204
int __var1;
195205
double __var2;
196206
string __var3;
197207
alias variables = AliasSeq!(__var1, __var2, __var3);
198-
*/
199208
---
200209

201-
$(P This is also what happens when declaring a variadic template function:)
210+
$(P So a type sequence instance is a kind of symbol sequence that only aliases
211+
variables, known as an $(I lvalue sequence).)
212+
213+
$(P $(DDSUBLINK spec/template, variadic-templates, Variadic template functions)
214+
use a type sequence instance for a function parameter:)
202215

203216
---
204217
void foo(T...)(T args)
205218
{
206-
// 'args' here is a compile-time list of symbols that
207-
// refer to underlying compiler-generated arguments
219+
// 'T' is a type sequence of argument types.
220+
// 'args' is an instance of T whose elements are the function parameters
221+
static assert(is(typeof(args) == T));
222+
args[0] = T[0].init;
208223
}
209224
---
210225

211-
$(P It is possible to use expression lists with values of the same type to declare array literals:)
226+
$(P $(B Note:) $(REF Tuple, std, typecons) wraps a type sequence instance,
227+
preventing auto-expansion, and allowing it to be returned from functions.)
228+
229+
$(H3 $(LNAME2 value-seq, Value sequences))
230+
231+
$(P It is possible to use a sequence of values of the same type to declare an array literal:)
212232

213233
---
214234
import std.meta;
215-
static assert ([ AliasSeq!(1, 2, 3) ] == [ 1, 2, 3 ]);
235+
enum e = 3;
236+
enum arr = [ AliasSeq!(1, 2, e) ];
237+
static assert(arr == [ 1, 2, 3 ]);
238+
---
239+
240+
$(P The above sequence is an $(I rvalue sequence), as it is comprised only of literal values
241+
and manifest constants. Each element's value is known at compile-time.)
242+
243+
$(P The following demonstrates use of an $(I lvalue sequence):)
216244
---
245+
import std.meta, std.algorithm;
246+
auto x = 3, y = 7;
247+
alias pair = AliasSeq!(x, y);
248+
swap(pair);
249+
assert(x == 7 && y == 3);
250+
---
251+
$(P As above, such sequences may use $(ALOCAL assignment, assignment).)
252+
253+
$(H4 $(LNAME2 tupleof, Aggregate field sequences))
254+
255+
$(P $(DDSUBLINK spec/class, class_properties, `.tupleof`) is a
256+
class/struct instance property that provides an lvalue sequence
257+
of each field.)
258+
259+
$(H3 $(LNAME2 symbol-seq, Symbol sequences))
260+
261+
$(P A symbol sequence aliases any named symbol - types, variables, functions and templates -
262+
but not literals.
263+
Like an alias sequence, the kind of elements can be mixed.)
217264

218265
)
219266

220267
Macros:
221-
TITLE=Compile-time Argument Lists
268+
TITLE=Compile-time Sequences
222269
SUBNAV=$(SUBNAV_ARTICLES)

articles/index.dd

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,9 @@ $(D_S Articles,
129129
templates.)
130130
)
131131
$(DIVC item,
132-
$(H4 $(LINK2 $(ROOT_DIR)articles/ctarguments.html, Compile-time Argument
133-
Lists))
134-
$(P A compile-time list is a list of compile-time entities -
135-
types, symbols (names) and expressions (values). This
132+
$(H4 $(LINK2 $(ROOT_DIR)articles/ctarguments.html, Compile-time Sequences))
133+
$(P A compile-time sequence is a sequence of compile-time entities -
134+
types, symbols (names) and values. This
136135
article shows how to work with them.)
137136
)
138137
)

dlang.org.ddoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ $(SUBNAV_TEMPLATE
475475
$(ROOT_DIR)articles/regular-expression.html, Regular Expressions,
476476
$(ROOT_DIR)articles/safed.html, SafeD,
477477
$(ROOT_DIR)articles/templates-revisited.html, Templates Revisited,
478-
$(ROOT_DIR)articles/ctarguments.html, Compile-time Argument Lists,
478+
$(ROOT_DIR)articles/ctarguments.html, Compile-time Sequences,
479479
$(ROOT_DIR)articles/variadic-function-templates.html, Variadic Templates,
480480
$(ROOT_DIR)articles/d-array-article.html, D Slices,
481481
$(ROOT_DIR)articles/cppcontracts.html, D's Contract Programming,

tuple.dd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Ddoc
33
For documentation on tuples that are commonly present in other languages (anonymous runtime entity that groups
44
different values) refer to $(LINK2 phobos/std_typecons.html#tuple, Phobos documentation on std.typecons.tuple)
55

6-
You may be also looking for documentation for built-in $(LINK2 ctarguments.html, compile-time argument lists)
6+
You may be also looking for documentation for built-in $(LINK2 ctarguments.html, compile-time sequences)
77
which can be sometimes referred as "tuples" too (though it is discouraged).
88

99
Macros:

0 commit comments

Comments
 (0)