I'm experimenting on a serializer design. The idea includes a
shorthand for processing a field and its attributes with an
alias. But I'm not sure whether I can gather enough information
from it, such as the name of the field. apparently F.stringof
stands for "this.\<fieldname\>" in this context. So how can I get
only the field's name? And is it possible to check if F is a
field? -- In a well defined manner.
import std.stdio;
void processField(alias F)(Serializer sz) // is field?
{
writeln("attrs ", __traits(getAttributes, F)); // OK
writeln(F.stringof); // "this.intField"
sz.process(F.stringof, F); // "serializing int this.intField: 0"
}
class Serializer
{
void process(string k, ref int v)
{
writeln("serializing int ", k, ": ", v);
}
void process(string k, ref long v)
{
writeln("serializing long ", k, ": ", v);
}
void process(U)(ref U v) if (is(U == struct))
{
v.serialize(this);
}
}
struct OldName
{
string name;
}
struct Serializable
{
@OldName("ifield")
int intField;
void serialize(Serializer sz)
{
sz.processField!intField();
}
}
void main()
{
import std.stdio;
import std.traits;
import core.internal.traits;
Serializable v;
auto sz = new Serializer();
sz.process(v);
}