, , , :
http://dlang.org/phobos/std_traits.html#Fields
, . CatStruct . :
mixin template CatStruct(string name, T...) {
static import std.traits, std.conv;
private string _code_generator() {
string code = "struct " ~ name ~ " {";
foreach(oidx, t; T) {
foreach(idx, field; std.traits.FieldTypeTuple!t)
code ~= "std.traits.FieldTypeTuple!(T["~std.conv.to!string(oidx)~"])["~std.conv.to!string(idx)~"] "~ std.traits.FieldNameTuple!t[idx] ~ ";";
}
code ~= "}";
return code;
}
mixin(_code_generator());
}
mix mixin... mixins , . , , , .
, , , , , opDispatch, ( , , ).
, , . mixin - mixin, . . , , .
FieldTypeTuple FieldNameTuple stdlib, , , , .
, , , , . ( alias this, -.)
, , , , code ~=, . code ~= field.stringof ~ " "~ FieldNameTuple!t[idx] ~ ";";
tl; dr: , , , mixin() . /
. - FieldTypeTuple, field.stringof - .
, struct A - .
struct A { import arsd.color; Color a; }
AB ab;
import arsd.color;
ab.a = Color.white; ab.b = 2;
struct A, mixin.
mixin,
// comment fancy line
// code ~= "std.traits.FieldTypeTuple!(T["~std.conv.to!string(oidx)~"])["~std.conv.to!string(idx)~"] "~ std.traits.FieldNameTuple!t[idx] ~ ";";
// paste in simple line
code ~= field.stringof ~ " "~ std.traits.FieldNameTuple!t[idx] ~ ";";
:
$ dmd f.d ~/arsd/color.d
f.d-mixin-31(31): Error: undefined identifier 'Color'
f.d(4): Error: mixin f.CatStruct!("AB", A, B) error instantiating
Zoinks! , "". - Color , .... :
struct A { import arsd.color; Color a; }
struct B { int b; }
struct Color { static Color white() { return Color.init; } }
mixin CatStruct!("AB", A, B);
AB ab;
import arsd.color;
ab.a = Color.white; ab.b = 2;
:
$ dmd f.d ~/arsd/color.d
f.d(12): Error: cannot implicitly convert expression (white()) of type Color to Color
: , - - , " ", : .
, , , .
FieldTypeTuple, , , . , , .
, , , , , , . - , !:) , , , -.