FieldTypes

Evaluates to an AliasSeq of the types of the member variables of an aggregate type (i.e. a struct, class, interface, or union).

These are fields which take up memory space within an instance of the type (i.e. not enums / manifest constants, since they don't take up memory space, and not static member variables, since they don't take up memory space within an instance).

Hidden fields (like the virtual function table pointer or the context pointer for nested types) are not included.

For classes, only the direct member variables are included and not those of any base classes.

For interfaces, the result of FieldTypes is always empty, because interfaces cannot have member variables. However, because interfaces are aggregate types, they work with FieldTypes for consistency so that code that's written to work on aggregate types doesn't have to worry about whether it's dealing with an interface.

template FieldTypes (
T
) if (
isAggregateType!T
) {}

Examples

1 import phobos.sys.meta : AliasSeq;
2 
3 struct S
4 {
5     int x;
6     float y;
7 }
8 static assert(is(FieldTypes!S == AliasSeq!(int, float)));
9 
10 // Type qualifers will be passed on to the result.
11 static assert(is(FieldTypes!(const S) == AliasSeq!(const int, const float)));
12 static assert(is(FieldTypes!(shared S) == AliasSeq!(shared int, shared float)));
13 
14 class C
15 {
16     // static variables are not included.
17     static int var;
18 
19     // Manifest constants are not included.
20     enum lang = "dlang";
21 
22     // Functions are not included, even if they're @property functions.
23     @property int foo() { return 42; }
24 
25     string s;
26     int i;
27     int[] arr;
28 }
29 static assert(is(FieldTypes!C == AliasSeq!(string, int, int[])));
30 
31 // Only direct member variables are included. Member variables from any base
32 // classes are not.
33 class D : C
34 {
35     real r;
36 }
37 static assert(is(FieldTypes!D == AliasSeq!real));
38 
39 // FieldTypes will always be empty for an interface, since it's not legal
40 // for interfaces to have member variables.
41 interface I
42 {
43 }
44 static assert(FieldTypes!I.length == 0);
45 
46 union U
47 {
48     int i;
49     double d;
50     long l;
51     S s;
52 }
53 static assert(is(FieldTypes!U == AliasSeq!(int, double, long, S)));
54 
55 // FieldTypes only operates on aggregate types.
56 static assert(!__traits(compiles, FieldTypes!int));
57 static assert(!__traits(compiles, FieldTypes!(S*)));
58 static assert(!__traits(compiles, FieldTypes!(C[])));

See Also

Meta