Skip to content

Commit cbf0c23

Browse files
authored
[articles/ctarguments.dd] Various improvements (#3207)
Use CONSOLE macro. Use lowercase identifiers for value sequences. Change 'Assignment' heading to 'Element assignment' and make it a subheading of 'Indexing and slicing'. Change 'Loops' to 'foreach'. Change 'Type sequence instantiation' to not be a subheading of type sequences because it's about (l)value sequences, the next heading. Move Tuple sentence to 'Value sequences' heading (it applies to other value sequences as well besides just type sequence instances). Remove unnecessary 'Aggregate field sequences' subheading. Mention value sequences can't be returned from functions. Add symbol sequence example.
1 parent f0c4323 commit cbf0c23

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

articles/ctarguments.dd

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,24 @@ $(HEADERNAV_TOC)
3636
---
3737
template Variadic(T...)
3838
{
39-
static assert (T.length > 1);
39+
static assert(T.length > 1);
4040
pragma(msg, T[0]);
4141
pragma(msg, T[1]);
4242
}
4343

4444
alias dummy = Variadic!(int, 42);
45-
// prints during compilation:
46-
// int
47-
// 42
4845
---
46+
$(P Output during compilation:)
47+
$(CONSOLE
48+
int
49+
42
50+
)
4951

5052
$(H2 $(LNAME2 AliasSeq, AliasSeq))
5153

5254
$(P The language itself does not provide any means to define such sequences outside of
5355
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
5557
library:
5658
)
5759

@@ -79,7 +81,7 @@ $(H2 $(LNAME2 available-operations, Available operations))
7981

8082
---
8183
import std.meta;
82-
static assert (AliasSeq!(1, 2, 3, 4).length == 4);
84+
static assert(AliasSeq!(1, 2, 3, 4).length == 4);
8385
---
8486

8587
$(H3 $(LNAME2 indexing-slicing, Indexing and slicing))
@@ -88,13 +90,17 @@ $(H2 $(LNAME2 available-operations, Available operations))
8890

8991
---
9092
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);
95101
---
96102

97-
$(H3 $(LNAME2 assignment, Assignment))
103+
$(H4 $(LNAME2 assignment, Element assignment))
98104

99105
$(P Works only if the sequence element is a symbol that refers to a mutable variable)
100106

@@ -106,12 +112,12 @@ $(H2 $(LNAME2 available-operations, Available operations))
106112
int x;
107113
alias seq = AliasSeq!(10, x);
108114
seq[1] = 42;
109-
assert (x == 42);
115+
assert(x == 42);
110116
// seq[0] = 42; // won't compile, can't assign to a constant
111117
}
112118
---
113119

114-
$(H3 $(LNAME2 loops, Loops))
120+
$(H3 $(LNAME2 loops, `foreach`))
115121

116122
$(P D's $(DDSUBLINK spec/statement, ForeachStatement, foreach statement) has special
117123
semantics when iterating over compile-time sequences. It repeats the body of the loop
@@ -132,13 +138,13 @@ $(H2 $(LNAME2 available-operations, Available operations))
132138
pragma (msg, typeof(sym));
133139
}
134140
}
135-
136-
/* Prints:
137-
int
138-
string
139-
void()
140-
*/
141141
---
142+
$(P Output during compilation:)
143+
$(CONSOLE
144+
int
145+
string
146+
void()
147+
)
142148

143149
$(P $(B Note:) $(DDSUBLINK version, staticforeach, Static foreach)
144150
should be preferred in new code.)
@@ -182,9 +188,11 @@ $(H3 $(LNAME2 type-seq, Type sequences))
182188
import std.meta;
183189
alias Params = AliasSeq!(int, double, string);
184190
void foo(Params); // void foo(int, double, string);
191+
192+
foo(7, 6.5, "hi");
185193
---
186194

187-
$(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
195+
$(H3 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
188196

189197
$(P D supports a special variable declaration syntax where a type sequence acts as a type:)
190198

@@ -211,7 +219,7 @@ $(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
211219
variables, known as an $(I lvalue sequence).)
212220

213221
$(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:)
215223

216224
---
217225
void foo(T...)(T args)
@@ -223,9 +231,6 @@ $(H4 $(LNAME2 type-seq-instantiation, Type sequence instantiation))
223231
}
224232
---
225233

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-
229234
$(H3 $(LNAME2 value-seq, Value sequences))
230235

231236
$(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))
248253
swap(pair);
249254
assert(x == 7 && y == 3);
250255
---
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).)
254258

255259
$(P $(DDSUBLINK spec/class, class_properties, `.tupleof`) is a
256260
class/struct instance property that provides an lvalue sequence
257261
of each field.)
258262

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+
259267
$(H3 $(LNAME2 symbol-seq, Symbol sequences))
260268

261269
$(P A symbol sequence aliases any named symbol - types, variables, functions and templates -
262270
but not literals.
263271
Like an alias sequence, the kind of elements can be mixed.)
264272

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.)
265291
)
266292

267293
Macros:

0 commit comments

Comments
 (0)