On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote:
Given
class C
{
// lots of members
}
and a function
f(C c)
{
}
is there a generic way, perhaps through reflection, to reset
(inside f) all members of `c` to their default values?
Something along
foreach(ref member; __traits(allMembers, c))
{
member = typeof(member).init;
}
import std.traits;
foreach(i, member; FieldNameTuple!C)
{
alias FieldType = Fields!C[i];
static if(isMutable!FieldType)
__traits(getMember, c, member) = FieldType.init;
}
However, it doesn't work in the presence of private fields. A
better alternative is probably to `destroy` the instance then
`emplace` to default-construct a new instance over it.