forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtraits.dd
495 lines (386 loc) · 9.48 KB
/
traits.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
Ddoc
$(SPEC_S Traits,
$(P Traits are extensions to the language to enable
programs, at compile time, to get at information
internal to the compiler. This is also known as
compile time reflection.
It is done as a special, easily extended syntax (similar
to Pragmas) so that new capabilities can be added
as required.
)
$(GRAMMAR
$(GNAME TraitsExpression):
$(B __traits) $(B $(LPAREN)) $(I TraitsKeyword) $(B ,) $(I TraitsArguments) $(B $(RPAREN))
$(GNAME TraitsKeyword):
$(B isAbstractClass)
$(B isArithmetic)
$(B isAssociativeArray)
$(B isFinalClass)
$(B isFloating)
$(B isIntegral)
$(B isScalar)
$(B isStaticArray)
$(B isUnsigned)
$(B isVirtualFunction)
$(B isAbstractFunction)
$(B isFinalFunction)
$(B hasMember)
$(B getMember)
$(B getVirtualFunctions)
$(B classInstanceSize)
$(B allMembers)
$(B derivedMembers)
$(B isSame)
$(B compiles)
$(GNAME TraitsArguments):
$(I TraitsArgument)
$(I TraitsArgument) $(B ,) $(I TraitsArguments)
$(GNAME TraitsArgument):
$(I AssignExpression)
$(I Type)
)
$(H2 isArithmetic)
$(P If the arguments are all either types that are arithmetic types,
or expressions that are typed as arithmetic types, then $(B true)
is returned.
Otherwise, $(B false) is returned.
If there are no arguments, $(B false) is returned.)
---
import std.stdio;
void main()
{
int i;
writefln(__traits(isArithmetic, int));
writefln(__traits(isArithmetic, i, i+1, int));
writefln(__traits(isArithmetic));
writefln(__traits(isArithmetic, int*));
}
---
$(P Prints:)
$(CONSOLE
true
true
false
false
)
$(H2 isFloating)
$(P Works like $(B isArithmetic), except it's for floating
point types (including imaginary and complex types).)
$(H2 isIntegral)
$(P Works like $(B isArithmetic), except it's for integral
types (including character types).)
$(H2 isScalar)
$(P Works like $(B isArithmetic), except it's for scalar
types.)
$(H2 isUnsigned)
$(P Works like $(B isArithmetic), except it's for unsigned
types.)
$(H2 isStaticArray)
$(P Works like $(B isArithmetic), except it's for static array
types.)
$(H2 isAssociativeArray)
$(P Works like $(B isArithmetic), except it's for associative array
types.)
$(H2 isAbstractClass)
$(P If the arguments are all either types that are abstract classes,
or expressions that are typed as abstract classes, then $(B true)
is returned.
Otherwise, $(B false) is returned.
If there are no arguments, $(B false) is returned.)
---
import std.stdio;
abstract class C { int foo(); }
void main()
{
C c;
writefln(__traits(isAbstractClass, C));
writefln(__traits(isAbstractClass, c, C));
writefln(__traits(isAbstractClass));
writefln(__traits(isAbstractClass, int*));
}
---
$(P Prints:)
$(CONSOLE
true
true
false
false
)
$(H2 isFinalClass)
$(P Works like $(B isAbstractClass), except it's for final
classes.)
$(H2 isVirtualFunction)
$(P Takes one argument. If that argument is a virtual function,
$(B true) is returned, otherwise $(B false).
)
---
import std.stdio;
struct S
{
void bar() { }
}
class C
{
void bar() { }
}
void main()
{
writefln(__traits(isVirtualFunction, C.bar)); // true
writefln(__traits(isVirtualFunction, S.bar)); // false
}
---
$(H2 isAbstractFunction)
$(P Takes one argument. If that argument is an abstract function,
$(B true) is returned, otherwise $(B false).
)
---
import std.stdio;
struct S
{
void bar() { }
}
class C
{
void bar() { }
}
class AC
{
abstract void foo();
}
void main()
{
writefln(__traits(isAbstractFunction, C.bar)); // false
writefln(__traits(isAbstractFunction, S.bar)); // false
writefln(__traits(isAbstractFunction, AC.foo)); // true
}
---
$(H2 isFinalFunction)
$(P Takes one argument. If that argument is a final function,
$(B true) is returned, otherwise $(B false).
)
---
import std.stdio;
struct S
{
void bar() { }
}
class C
{
void bar() { }
final void foo();
}
final class FC
{
void foo();
}
void main()
{
writefln(__traits(isFinalFunction, C.bar)); // false
writefln(__traits(isFinalFunction, S.bar)); // false
writefln(__traits(isFinalFunction, C.foo)); // true
writefln(__traits(isFinalFunction, FC.foo)); // true
}
---
$(H2 hasMember)
$(P The first argument is a type that has members, or
is an expression of a type that has members.
The second argument is a string.
If the string is a valid property of the type,
$(B true) is returned, otherwise $(B false).
)
---
import std.stdio;
struct S
{
int m;
}
void main()
{ S s;
writefln(__traits(hasMember, S, "m")); // true
writefln(__traits(hasMember, s, "m")); // true
writefln(__traits(hasMember, S, "y")); // false
writefln(__traits(hasMember, int, "sizeof")); // true
}
---
$(H2 getMember)
$(P Takes two arguments, the second must be a string.
The result is an expression formed from the first
argument, followed by a '.', followed by the second
argument as an identifier.
)
---
import std.stdio;
struct S
{
int mx;
static int my;
}
void main()
{ S s;
__traits(getMember, s, "mx") = 1; // same as s.mx=1;
writefln(__traits(getMember, s, "m" ~ "x")); // 1
__traits(getMember, S, "mx") = 1; // error, no this for S.mx
__traits(getMember, S, "my") = 2; // ok
}
---
$(H2 getVirtualFunctions)
$(P The first argument is a class type or an expression of
class type.
The second argument is a string that matches the name of
one of the functions of that class.
The result is an array of the virtual overloads of that function.
)
---
import std.stdio;
class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 2; }
}
void main()
{
D d = new D();
foreach (t; __traits(getVirtualFunctions, D, "foo"))
writefln(typeid(typeof(t)));
alias typeof(__traits(getVirtualFunctions, D, "foo")) b;
foreach (t; b)
writefln(typeid(t));
auto i = __traits(getVirtualFunctions, d, "foo")[1](1);
writefln(i);
}
---
$(P Prints:)
$(CONSOLE
void()
int()
void()
int()
2
)
$(H2 classInstanceSize)
$(P Takes a single argument, which must evaluate to either
a class type or an expression of class type.
The result
is of type $(CODE size_t), and the value is the number of
bytes in the runtime instance of the class type.
It is based on the static type of a class, not the
polymorphic type.
)
$(H2 allMembers)
$(P Takes a single argument, which must evaluate to either
a type or an expression of type.
An array of string literals is returned, each of which
is the name of a member of that type combined with all
of the members of the base classes (if the class is a type).
No name is repeated.
Builtin properties are not included.
)
---
import std.stdio;
class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 0; }
}
void main()
{
auto a = __traits(allMembers, D);
writefln(a);
// [_ctor,_dtor,foo,print,toString,toHash,opCmp,opEquals]
}
---
$(P The order in which the strings appear in the result
is not defined.)
$(H2 derivedMembers)
$(P Takes a single argument, which must evaluate to either
a type or an expression of type.
An array of string literals is returned, each of which
is the name of a member of that type.
No name is repeated.
Base class member names are not included.
Builtin properties are not included.
)
---
import std.stdio;
class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 0; }
}
void main()
{
auto a = __traits(derivedMembers, D);
writefln(a); // [_ctor,_dtor,foo]
}
---
$(P The order in which the strings appear in the result
is not defined.)
$(H2 isSame)
$(P Takes two arguments and returns bool $(B true) if they
are the same symbol, $(B false) if not.)
---
import std.stdio;
struct S { }
int foo();
int bar();
void main()
{
writefln(__traits(isSame, foo, foo)); // true
writefln(__traits(isSame, foo, bar)); // false
writefln(__traits(isSame, foo, S)); // false
writefln(__traits(isSame, S, S)); // true
writefln(__traits(isSame, std, S)); // false
writefln(__traits(isSame, std, std)); // true
}
---
$(P If the two arguments are expressions made up of literals
or enums that evaluate to the same value, true is returned.)
$(H2 compiles)
$(P Returns a bool $(B true) if all of the arguments
compile (are semantically correct).
The arguments can be symbols, types, or expressions that
are syntactically correct.
The arguments cannot be statements or declarations.
)
$(P If there are no arguments, the result is $(B false).)
---
import std.stdio;
struct S
{
static int s1;
int s2;
}
int foo();
int bar();
void main()
{
writefln(__traits(compiles)); // false
writefln(__traits(compiles, foo)); // true
writefln(__traits(compiles, foo + 1)); // true
writefln(__traits(compiles, &foo + 1)); // false
writefln(__traits(compiles, typeof(1))); // true
writefln(__traits(compiles, S.s1)); // true
writefln(__traits(compiles, S.s3)); // false
writefln(__traits(compiles, 1,2,3,int,long,std)); // true
writefln(__traits(compiles, 3[1])); // false
writefln(__traits(compiles, 1,2,3,int,long,3[1])); // false
}
---
$(P This is useful for:)
$(UL
$(LI Giving better error messages inside generic code than
the sometimes hard to follow compiler ones.)
$(LI Doing a finer grained specialization than template
partial specialization allows for.)
)
)
Macros:
TITLE=Traits
WIKI=Traits
H2=<h2>$0</h2>