| |
 | Posted by Nick Treleaven in reply to Andy Valencia | Permalink Reply |
|
Nick Treleaven 
Posted in reply to Andy Valencia
| On Friday, 27 June 2025 at 22:33:25 UTC, Andy Valencia wrote:
> tupleof works for struct and class instances, but explicitly documents that it ignores instance variables from any superclass.
Is there any way to enumerate all the instance variables, not just the ones present in the specific instance's class?
Thanks,
Andy
This is one way:
import std;
class C
{
int x, y;
}
class D : C
{
int a, b;
}
void main()
{
D d = new D;
static if (is(D Bases == super))
{
static foreach (B; AliasSeq!(D, Bases))
{
pragma(msg, B);
static foreach (field; B.tupleof)
{{
enum s = __traits(identifier, field);
pragma(msg, s);
// access runtime field
__traits(getMember, d, s)++;
}}
}
}
assert([d.tupleof, (cast(C)d).tupleof] == [1,1,1,1]);
}
Pragma output:
D
a
b
C
x
y
|