xxxxxxxxxx
import std;
import std.stdio;
struct Foo
{
string bar;
string baz;
}
struct Builder
{
Foo f;
ref Builder bar(string s)
{
enforce (s != f.baz);
f.bar = s;
return this;
}
import std.traits;
import std.format;
static foreach (i, F; Fields!Foo)
{
static if (true) // only if no member FieldNameTuple!Foo[i] alrdeay exists
{
mixin(format("ref Builder %2$s(%1$s __v) return { t.%2$s = __v; return this; }",
F.stringof, FieldNameTuple!Foo[i]));
}
}
Foo build() { return f; }
}
void main()
{
Foo f = Builder
.init
.bar("bar")
.baz("baz")
.build;
writeln(f);
}