@@ -36,22 +36,24 @@ $(HEADERNAV_TOC)
36
36
---
37
37
template Variadic(T...)
38
38
{
39
- static assert (T.length > 1);
39
+ static assert(T.length > 1);
40
40
pragma(msg, T[0]);
41
41
pragma(msg, T[1]);
42
42
}
43
43
44
44
alias dummy = Variadic!(int, 42);
45
- // prints during compilation:
46
- // int
47
- // 42
48
45
---
46
+ $(P Output during compilation:)
47
+ $(CONSOLE
48
+ int
49
+ 42
50
+ )
49
51
50
52
$(H2 $(LNAME2 AliasSeq, AliasSeq))
51
53
52
54
$(P The language itself does not provide any means to define such sequences outside of
53
55
a template parameter declaration. Instead, there is a
54
- $(MREF_ALTTEXT simple utility, std, meta) provided by the standard
56
+ $(REF_ALTTEXT simple template, AliasSeq, std, meta) provided by the standard
55
57
library:
56
58
)
57
59
@@ -79,7 +81,7 @@ $(H2 $(LNAME2 available-operations, Available operations))
79
81
80
82
---
81
83
import std.meta;
82
- static assert (AliasSeq!(1, 2, 3, 4).length == 4);
84
+ static assert(AliasSeq!(1, 2, 3, 4).length == 4);
83
85
---
84
86
85
87
$(H3 $(LNAME2 indexing-slicing, Indexing and slicing))
@@ -88,13 +90,17 @@ $(H2 $(LNAME2 available-operations, Available operations))
88
90
89
91
---
90
92
import std.meta;
91
- alias Numbers = AliasSeq!(1, 2, 3, 4);
92
- static assert (Numbers[1] == 2);
93
- alias SubNumbers = Numbers[1 .. $];
94
- static assert (SubNumbers[0] == 2);
93
+
94
+ alias nums = AliasSeq!(1, 2, 3, 4);
95
+ static assert(nums[1] == 2);
96
+
97
+ // slice last 3 elements
98
+ alias tail = nums[1 .. $];
99
+ static assert(tail[0] == 2);
100
+ static assert(tail.length == 3);
95
101
---
96
102
97
- $(H3 $(LNAME2 assignment, Assignment ))
103
+ $(H4 $(LNAME2 assignment, Element assignment ))
98
104
99
105
$(P Works only if the sequence element is a symbol that refers to a mutable variable)
100
106
@@ -106,12 +112,12 @@ $(H2 $(LNAME2 available-operations, Available operations))
106
112
int x;
107
113
alias seq = AliasSeq!(10, x);
108
114
seq[1] = 42;
109
- assert (x == 42);
115
+ assert(x == 42);
110
116
// seq[0] = 42; // won't compile, can't assign to a constant
111
117
}
112
118
---
113
119
114
- $(H3 $(LNAME2 loops, Loops ))
120
+ $(H3 $(LNAME2 loops, `foreach` ))
115
121
116
122
$(P D's $(DDSUBLINK spec/statement, ForeachStatement, foreach statement) has special
117
123
semantics when iterating over compile-time sequences. It repeats the body of the loop
@@ -132,13 +138,13 @@ $(H2 $(LNAME2 available-operations, Available operations))
132
138
pragma (msg, typeof(sym));
133
139
}
134
140
}
135
-
136
- /* Prints:
137
- int
138
- string
139
- void()
140
- */
141
141
---
142
+ $(P Output during compilation:)
143
+ $(CONSOLE
144
+ int
145
+ string
146
+ void()
147
+ )
142
148
143
149
$(P $(B Note:) $(DDSUBLINK version, staticforeach, Static foreach)
144
150
should be preferred in new code.)
@@ -182,9 +188,11 @@ $(H3 $(LNAME2 type-seq, Type sequences))
182
188
import std.meta;
183
189
alias Params = AliasSeq!(int, double, string);
184
190
void foo(Params); // void foo(int, double, string);
191
+
192
+ foo(7, 6.5, "hi");
185
193
---
186
194
187
- $(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
195
+ $(H3 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
188
196
189
197
$(P D supports a special variable declaration syntax where a type sequence acts as a type:)
190
198
@@ -211,7 +219,7 @@ $(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
211
219
variables, known as an $(I lvalue sequence).)
212
220
213
221
$(P $(DDSUBLINK spec/template, variadic-templates, Variadic template functions)
214
- use a type sequence instance for a function parameter :)
222
+ use a type sequence instance to declare function parameters :)
215
223
216
224
---
217
225
void foo(T...)(T args)
@@ -223,9 +231,6 @@ $(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
223
231
}
224
232
---
225
233
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
234
$(H3 $(LNAME2 value-seq, Value sequences))
230
235
231
236
$(P It is possible to use a sequence of values of the same type to declare an array literal:)
@@ -248,20 +253,41 @@ $(H3 $(LNAME2 value-seq, Value sequences))
248
253
swap(pair);
249
254
assert(x == 7 && y == 3);
250
255
---
251
- $(P As above, such sequences may use $(ALOCAL assignment, assignment).)
252
-
253
- $(H4 $(LNAME2 tupleof, Aggregate field sequences))
256
+ $(P As above, such sequences may use
257
+ $(ALOCAL assignment, element assignment).)
254
258
255
259
$(P $(DDSUBLINK spec/class, class_properties, `.tupleof`) is a
256
260
class/struct instance property that provides an lvalue sequence
257
261
of each field.)
258
262
263
+ $(P A function cannot return a value sequence. Instead,
264
+ $(REF Tuple, std, typecons) can be used. It wraps an lvalue
265
+ sequence in a struct, preventing auto-expansion.)
266
+
259
267
$(H3 $(LNAME2 symbol-seq, Symbol sequences))
260
268
261
269
$(P A symbol sequence aliases any named symbol - types, variables, functions and templates -
262
270
but not literals.
263
271
Like an alias sequence, the kind of elements can be mixed.)
264
272
273
+ ---
274
+ import std.meta : AliasSeq;
275
+
276
+ void f(T)(T v)
277
+ {
278
+ pragma(msg, T);
279
+ }
280
+ alias syms = AliasSeq!(f, f!byte);
281
+ syms[0](42); // call f!int with IFTI
282
+ syms[1](42); // call f!byte
283
+ ---
284
+ $(P Output during compilation:)
285
+ $(CONSOLE
286
+ byte
287
+ int
288
+ )
289
+ $(P Note that function `f!byte` is instantiated when the sequence is
290
+ created, not at the call-site.)
265
291
)
266
292
267
293
Macros:
0 commit comments