std.reflection prototype

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 15 08:26:48 PDT 2015


On Wed, 15 Apr 2015 05:31:24 -0400, Jacob Carlborg <doob at me.com> wrote:

>
> It needs to be possible to set and get a value of an instance variable  
> based on it's name, through runtime reflection. It also needs to bypass  
> protection, i.e. "private".


Right now, this is the def:

/**
  * Array of pairs giving the offset and type information for each
  * member in an aggregate.
  */
struct OffsetTypeInfo
{
     size_t   offset;    /// Offset of member from start of object
     TypeInfo ti;        /// TypeInfo for this member
}

If "string name" esd added, and then offTi[] esd actually populated, then  
I suppose you could do this:

class Test {
	int a = 4;
	private int b = 5;
	void print(){ writeln(b); }
}

void main()
{
     Test test = new Test;
     // offsetof would instead come from the TypeInfo/OffsetTypeInfo
     int* b = cast(int*)(cast(void*)test + Test.b.offsetof);
     *b = 1234;
     test.print();
}

But AFAIK, this is NOT ok in C++ because of the way inheritance works.. is  
this safe in D?


More information about the Digitalmars-d mailing list