/
template-mixin.dd
311 lines (249 loc) · 6.93 KB
/
template-mixin.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
Ddoc
$(SPEC_S Template Mixins,
A $(I TemplateMixin) takes an arbitrary set of declarations from
the body of a $(I TemplateDeclaration) and inserts them
into the current context.
$(GRAMMAR
$(GNAME TemplateMixinDeclaration):
$(V2 $(B mixin)) $(B template) $(TEMPLATEIDENTIFIER) $(B $(LPAREN)) $(TEMPLATEPARAMETERLIST) $(B $(RPAREN)) $(V2 $(GLINK2 template, Constraint)$(SUB $(I opt)))
$(B {) $(GLINK2 module, DeclDefs) $(B })
$(GNAME TemplateMixin):
$(B mixin) $(TEMPLATEIDENTIFIER) $(B ;)
$(B mixin) $(TEMPLATEIDENTIFIER) $(GLINK MixinIdentifier) $(B ;)
$(B mixin) $(TEMPLATEIDENTIFIER) $(B !$(LPAREN)) $(TEMPLATEARGUMENTLIST) $(B $(RPAREN)) $(B ;)
$(B mixin) $(TEMPLATEIDENTIFIER) $(B !$(LPAREN)) $(TEMPLATEARGUMENTLIST) $(B $(RPAREN)) $(GLINK MixinIdentifier) $(B ;)
$(GNAME MixinIdentifier):
$(I Identifier)
)
$(P A $(I TemplateMixin) can occur in declaration lists of modules,
classes, structs, unions, and as a statement.
The $(I TemplateIdentifier) refers to a $(I TemplateDeclaration).
If the $(I TemplateDeclaration) has no parameters, the mixin
form that has no !($(I TemplateArgumentList))
can be used.
)
$(P Unlike a template instantiation, a template mixin's body is evaluated
within the scope where the mixin appears, not where the template declaration
is defined. It is analogous to cutting and pasting the body of
the template into the location of the mixin. It is useful for injecting
parameterized $(SINGLEQUOTE boilerplate) code, as well as for creating
templated nested functions, which is not possible with
template instantiations.
)
------
$(V2 mixin) template Foo() {
int x = 5;
}
$(B mixin Foo;)
struct Bar {
$(B mixin Foo;)
}
void test() {
writefln("x = %d", x); // prints 5
{
Bar b;
int x = 3;
writefln("b.x = %d", b.x); // prints 5
writefln("x = %d", x); // prints 3
{
$(B mixin Foo;)
writefln("x = %d", x); // prints 5
x = 4;
writefln("x = %d", x); // prints 4
}
writefln("x = %d", x); // prints 3
}
writefln("x = %d", x); // prints 5
}
------
Mixins can be parameterized:
------
$(V2 mixin) template Foo(T) {
T x = 5;
}
$(B mixin Foo!(int);) // create x of type int
------
Mixins can add virtual functions to a class:
------
$(V2 mixin) template Foo() {
void func() { writefln("Foo.func()"); }
}
class Bar {
$(B mixin Foo);
}
class Code : Bar {
$(V2 override) void func() { writefln("Code.func()"); }
}
void test() {
Bar b = new Bar();
b.func(); // calls Foo.func()
b = new Code();
b.func(); // calls Code.func()
}
------
Mixins are evaluated in the scope of where they appear, not the scope
of the template declaration:
------
int y = 3;
$(V2 mixin) template Foo() {
int abc() { return y; }
}
void test() {
int y = 8;
$(B mixin Foo;) // local y is picked up, not global y
assert(abc() == 8);
}
------
Mixins can parameterize symbols using alias parameters:
------
$(V2 mixin) template Foo(alias b) {
int abc() { return b; }
}
void test() {
int y = 8;
$(B mixin Foo!(y);)
assert(abc() == 8);
}
------
This example uses a mixin to implement a generic Duff's device
for an arbitrary statement (in this case, the arbitrary statement
is in bold). A nested function is generated as well as a
delegate literal, these can be inlined by the compiler:
------
$(V2 mixin) template duffs_device(alias id1, alias id2, alias s)
{
void duff_loop()
{
if (id1 < id2)
{
typeof(id1) n = (id2 - id1 + 7) / 8;
switch ((id2 - id1) % 8)
{
case 0: do { s(); $(V2 goto case;)
case 7: s(); $(V2 goto case;)
case 6: s(); $(V2 goto case;)
case 5: s(); $(V2 goto case;)
case 4: s(); $(V2 goto case;)
case 3: s(); $(V2 goto case;)
case 2: s(); $(V2 goto case;)
case 1: s(); continue;
default: assert(0, "Impossible");
} while (--n > 0);
}
}
}
}
void foo() { writefln("foo"); }
void test() {
int i = 1;
int j = 11;
mixin duffs_device!(i, j, $(B delegate { foo(); }) );
duff_loop(); // executes foo() 10 times
}
------
<h2>Mixin Scope</h2>
The declarations in a mixin are $(SINGLEQUOTE imported) into the surrounding
scope. If the name of a declaration in a mixin is the same
as a declaration in the surrounding scope, the surrounding declaration
overrides the mixin one:
------
int x = 3;
$(V2 mixin) template Foo() {
int x = 5;
int y = 5;
}
$(B mixin Foo;)
int y = 3;
void test() {
writefln("x = %d", x); // prints 3
writefln("y = %d", y); // prints 3
}
------
If two different mixins are put in the same scope, and each
define a declaration with the same name, there is an ambiguity
error when the declaration is referenced:
------
$(V2 mixin) template Foo() {
int x = 5;
void func(int x) { }
}
$(V2 mixin) template Bar() {
int x = 4;
void func() { }
}
$(B mixin Foo;)
$(B mixin Bar;)
void test() {
writefln("x = %d", x); // error, x is ambiguous
func(); // error, func is ambiguous
}
------
$(P The call to $(B func()) is ambiguous because
Foo.func and Bar.func are in different scopes.
)
$(P If a mixin has a $(I MixinIdentifier), it can be used to
disambiguate:
)
------
int x = 6;
$(V2 mixin) template Foo() {
int x = 5;
int y = 7;
void func() { }
}
$(V2 mixin) template Bar() {
int x = 4;
void func() { }
}
$(B mixin Foo F;)
$(B mixin Bar B;)
void test() {
writefln("y = %d", y); // prints 7
writefln("x = %d", x); // prints 6
writefln("F.x = %d", F.x); // prints 5
writefln("B.x = %d", B.x); // prints 4
F.func(); // calls Foo.func
B.func(); // calls Bar.func
}
------
$(P Alias declarations can be used to overload together
functions declared in different mixins:)
-----
$(V2 mixin) template Foo() {
void func(int x) { }
}
$(V2 mixin) template Bar() {
void func() { }
}
mixin Foo!() F;
mixin Bar!() B;
$(B alias F.func func;)
$(B alias B.func func;)
void main() {
func(); // calls B.func
func(1); // calls F.func
}
-----
$(P A mixin has its own scope, even if a declaration is overridden
by the enclosing one:)
------
int x = 4;
$(V2 mixin) template Foo() {
int x = 5;
int bar() { return x; }
}
$(B mixin Foo;)
void test() {
writefln("x = %d", x); // prints 4
writefln("bar() = %d", bar()); // prints 5
}
------
)
Macros:
TITLE=Mixins
WIKI=Mixin
CATEGORY_SPEC=$0
TEMPLATEIDENTIFIER=$(GLINK2 template, TemplateIdentifier)
TEMPLATEPARAMETERLIST=$(GLINK2 template, TemplateParameterList)
TEMPLATEARGUMENTLIST=$(GLINK2 template, TemplateArgumentList)
FOO=