exclude members at compile time
Alex
sascha.orlov at gmail.com
Sat Oct 7 19:56:49 UTC 2017
Ok, what I'm trying to do is the following:
take a type and a value of its type; given a known member of the
type, (re?)create a similar type, without this very known member.
What does work is this:
/// --- code ---
void main()
{
S s;
s.i = 42;
s.d = 73.0;
s.s.length = 5;
s.s[] = 1024;
s.c = 'c';
auto n = exclude!"i"(s);
static assert(!__traits(compiles, n.i));
assert(n.d == s.d);
assert(n.s == s.s);
assert(n.c == s.c);
n.s[0] = 55;
assert(n.s == s.s); // works, even by reference.
}
struct S
{
int i;
double d;
size_t[] s;
char c;
//auto fun(){} // <-- a function does not work, line 26
}
auto exclude(string without, T)(T t)
{
immutable b = [ __traits(allMembers, T) ];
struct Result
{
static foreach(i, _; b)
{
static if(without != b[i])
{
mixin(
typeof(__traits(getMember, T, b[i])).stringof ~
" " ~
__traits(getMember, T, b[i]).stringof ~
";"
);
}
}
}
Result res;
static foreach(i, _; b)
{
static if(without != b[i])
{
__traits(getMember, res, b[i]) = __traits(getMember, t, b[i]);
}
}
return res;
}
/// --- code ---
And the problem lies in line 26: If there is a method inside the
type, it won't work this way.
I assume, there should be a way, to copy the original type first
and to hide a member afterwards. Is there a simple approach for
this?
I think, the problem with my approach is, that if the member is
not a "data" member, than there are a plenty of possibilities to
check, like... function, template, well... there could be very
much types of indirections, as checking for "isCallable" is not
enough for recreation(?)
If these checks are the only way to handle this, than the answer
to my whole question is:
"recreation is restricted to PODs".
Then, I have to consider which checks to implement, and which
restrictions have to be applied to the input...
More information about the Digitalmars-d-learn
mailing list