www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Introspection - how?

reply Vincent <thornik gmail.com> writes:
Hello, guys!
I have a question related D's introspection. I try to solve serialization task
(implementing JSON protocol)
and stuck at very start: getting all class' fields with types, names and
values. Just test code:

    Thread d;
    foreach(mi; d.classinfo.getMembers(null))
13:     writefln(mi.name);

test.d(13): Error: function object.MemberInfo.name () does not match parameter
types ()
test.d(13): Error: mi.name can only be called on a mutable object, not
const(MemberInfo)

I'm confused with such strange error: what is a problem with using MemberInfo?
I just try to GET info, not WRITE!
Anybody have idea how to get info about members? (see task above)
Thanks.
May 19 2009
next sibling parent HOSOKAWA Kenchi <hskwk inter7.jp> writes:
Vincent Wrote:

 Hello, guys!
 I have a question related D's introspection. I try to solve serialization task
(implementing JSON protocol)
 and stuck at very start: getting all class' fields with types, names and
values. Just test code:
 
     Thread d;
     foreach(mi; d.classinfo.getMembers(null))
 13:     writefln(mi.name);
 
 test.d(13): Error: function object.MemberInfo.name () does not match parameter
types ()
 test.d(13): Error: mi.name can only be called on a mutable object, not
const(MemberInfo)
 
 I'm confused with such strange error: what is a problem with using MemberInfo?
I just try to GET info, not WRITE!
 Anybody have idea how to get info about members? (see task above)
 Thanks.
We face related problem; classinfo mutability. my post is article_id=90722. I have noticed that classinfo relates core of D's class system. http://www.digitalmars.com/d/2.0/phobos/object.html A pointer to this appears as the first entry in the class's vtbl[]. It seems that this problem must be treated carefully.
May 19 2009
prev sibling next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Vincent wrote:
 Hello, guys!
 I have a question related D's introspection. I try to solve serialization task
(implementing JSON protocol)
 and stuck at very start: getting all class' fields with types, names and
values. Just test code:
 
     Thread d;
     foreach(mi; d.classinfo.getMembers(null))
 13:     writefln(mi.name);
 
 test.d(13): Error: function object.MemberInfo.name () does not match parameter
types ()
 test.d(13): Error: mi.name can only be called on a mutable object, not
const(MemberInfo)
 
 I'm confused with such strange error: what is a problem with using MemberInfo?
I just try to GET info, not WRITE!
 Anybody have idea how to get info about members? (see task above)
 Thanks.
It doesn't matter; getMembers doesn't currently DO anything. Even if you cast away the const-ness, you'd get no results. You have to do this at compile-time with a concrete type for now. -- Daniel
May 19 2009
prev sibling parent Jacob Carlborg <doob me.com> writes:
Vincent wrote:
 Hello, guys!
 I have a question related D's introspection. I try to solve serialization task
(implementing JSON protocol)
 and stuck at very start: getting all class' fields with types, names and
values. Just test code:
 
     Thread d;
     foreach(mi; d.classinfo.getMembers(null))
 13:     writefln(mi.name);
 
 test.d(13): Error: function object.MemberInfo.name () does not match parameter
types ()
 test.d(13): Error: mi.name can only be called on a mutable object, not
const(MemberInfo)
 
 I'm confused with such strange error: what is a problem with using MemberInfo?
I just try to GET info, not WRITE!
 Anybody have idea how to get info about members? (see task above)
 Thanks.
You can use tupleof for classes and structs which returns a tuple containing all the fields of the class/struct. The following code will print the type, name and value of every field in the struct. BTW you can change private fields with tupleof. This is tested dmd v1.043 and Tango. struct Struct { private int i = 3; private char c = 'a'; } Struct s; foreach (i, t ; typeof(Struct.tupleof)) { const len = Struct.stringof.length; const fieldName = Struct.tupleof[i].stringof[1 + len + 2 .. $]; const typeName = typeof(Struct.tupleof[i]).stringof; Stdout.formatln("type: {0}", typeName); Stdout.formatln("name: {0}", fieldName); Stdout.formatln("value: {0}", s.tupleof[i]); Stdout().newline; } type: int name: i value: 3 type: char name: c value: a
May 19 2009