www.digitalmars.com         C & C++   DMDScript  

D - Reflection?

reply Patrick Down <pat codemoon.com> writes:
I thought I remember reading somewhere in the documentation that
D would support some degree of reflection like Java. ( The ablility
to inspect classes and structures at runtime to see the names, position 
and types of their members. ) But I can't find it though. Perhaps it 
was mentioned in the forums.  I hope this is true.  I think this is a
usefull feature.
Apr 05 2002
next sibling parent "Walter" <walter digitalmars.com> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns91E7AB18DAABBpatcodemooncom 63.105.9.61...
 I thought I remember reading somewhere in the documentation that
 D would support some degree of reflection like Java. ( The ablility
 to inspect classes and structures at runtime to see the names, position
 and types of their members. ) But I can't find it though. Perhaps it
 was mentioned in the forums.  I hope this is true.  I think this is a
 usefull feature.

It isn't there now, but it is anticipated to become part of the ClassInfo object that is available for each class type.
Apr 05 2002
prev sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns91E7AB18DAABBpatcodemooncom 63.105.9.61...
 I thought I remember reading somewhere in the documentation that
 D would support some degree of reflection like Java. ( The ablility
 to inspect classes and structures at runtime to see the names, position
 and types of their members. ) But I can't find it though. Perhaps it

I guess you won't see that for structures, since they are supposed to be lightweight; but for classes, there's ClassInfo, which already provides some useful information, like class name, its interfaces, and base class. If only we could construct an object by ClassInfo... (hint, hint!) =)
Apr 05 2002
parent reply Patrick Down <pat codemoon.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in
news:a8lrrc$25l5$2 digitaldaemon.com: 

 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns91E7AB18DAABBpatcodemooncom 63.105.9.61...
 I thought I remember reading somewhere in the documentation that
 D would support some degree of reflection like Java. ( The ablility
 to inspect classes and structures at runtime to see the names,
 position and types of their members. ) But I can't find it though.
 Perhaps it 

I guess you won't see that for structures, since they are supposed to be lightweight; but for classes, there's ClassInfo, which already provides some useful information, like class name, its interfaces, and base class.

I think that there needs to be some way to access this information for structures too. I think I remember Walter saying that he wants to use this information for optimizing the garbage collector and it's possible for strutures to have Object references too. Also to build things like an automatic object persistance you need to be able to completely navigate stuctures as well as classes.
 
 If only we could construct an object by ClassInfo... (hint, hint!) =)
 

I'd vote for that too.
Apr 05 2002
next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns91E7E72FCA314patcodemooncom 63.105.9.61...

 I think that there needs to be some way to access this information for
 structures too.  I think I remember Walter saying that he wants to use
 this information for optimizing the garbage collector and it's possible
 for strutures to have Object references too.

Well, structures can contain references to Object, so what? Adding classinfo pointer to each structure would alse make them incompatible with the API, since it expects normal C structs.
 Also to build things like an automatic object persistance you need to
 be able to completely navigate stuctures as well as classes.

I guess structures are better copied byte-by-byte, while classes would be more complicated. Those rare cases when struct contains reference to object don't worth the thing - if you really need it to be persistent, make it class, not struct.
Apr 06 2002
parent reply Patrick Down <pat codemoon.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in news:a8mk9t$n95$1
 digitaldaemon.com:

 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns91E7E72FCA314patcodemooncom 63.105.9.61...
 
 Adding classinfo pointer to each structure would alse make them
 incompatible with the API, since it expects normal C structs.

The information can be made available without doing anything to make stuctures incompatable. Unlike class references the compiler knows the real type of a stucture in all instances so a structure does not need to carry a extra pointer to a ClassInfo like classes do. All that is needed is a little extra atribute to access the information. struct Abc { } Abc astruct; ClassInfo b = astruct.type; In the line above the compiler knows that astruct is of type Abc so just subsitutes the pointer to Abc's ClassInfo in place of atruct.type.
Apr 06 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns91E86C7657A97patcodemooncom 63.105.9.61...

 All that is needed is a little extra atribute to access the
 information.

You are of course right. I should have thought a bit more about this =)
 struct Abc
 {
 }

 Abc astruct;
 ClassInfo b = astruct.type;

Since ClassInfo is class-specific and contains fields like destructor and vtbl[], it's probably better to define another class just for structs: class StructInfo { struct FieldInfo { char[] name; uint offset; ... } char[] name; uint[FieldInfo] fields; } And then: Foo foo; StructInfo fooinfo = foo.structinfo;
Apr 06 2002
parent reply "Walter" <walter digitalmars.com> writes:
Yeah, I was thinking along the lines of a structinfo. It's just not the same
animal as a class, and trying to mush the two together into classinfo seems
a mistake.

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8nk19$1qvo$1 digitaldaemon.com...
 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns91E86C7657A97patcodemooncom 63.105.9.61...

 All that is needed is a little extra atribute to access the
 information.

You are of course right. I should have thought a bit more about this =)
 struct Abc
 {
 }

 Abc astruct;
 ClassInfo b = astruct.type;

Since ClassInfo is class-specific and contains fields like destructor and vtbl[], it's probably better to define another class just for structs: class StructInfo { struct FieldInfo { char[] name; uint offset; ... } char[] name; uint[FieldInfo] fields; } And then: Foo foo; StructInfo fooinfo = foo.structinfo;

Apr 06 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
I love it when everyone agrees like this :)


--
Stijn
OddesE_XYZ hotmail.com
http://OddesE.cjb.net
_________________________________________________
Remove _XYZ from my address when replying by mail
Apr 07 2002
prev sibling parent reply Nuno Nunes <nachn altagis.biz> writes:
 I think the problem here is with the fact (From what I understood)

that there is no headers (.h) files in D.
Imagine you have a commercial library. If you do not have headers (and
you don't want to give the source code) there must be a way to the
programmer to know the functions, classes and structures in the library.
You may give it in the documentation, but it do not work for a RAD tool
with code completation (that is the best feature in those tools).
For what I see were you must rely all public interfaces in classes (for 
those will be possible for acess member information)
(I'm not saying thats a bad strategy)
Apr 06 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Nuno Nunes" <nachn altagis.biz> wrote in message
news:3CAF05AC.6030007 altagis.biz...
  I think the problem here is with the fact (From what I understood)

 that there is no headers (.h) files in D.
 Imagine you have a commercial library. If you do not have headers (and
 you don't want to give the source code) there must be a way to the
 programmer to know the functions, classes and structures in the library.
 You may give it in the documentation, but it do not work for a RAD tool
 with code completation (that is the best feature in those tools).

I wouldn't call class completion, no matter how much I like it, the best feature of Delphi... :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Apr 06 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:a8n4sq$19ub$1 digitaldaemon.com...

 I wouldn't call class completion, no matter how much I
 like it, the best feature of Delphi...  :)

Why not? By the way, Delphi is just an example of such approach - there are no headers there, and units can be in compiled form - and still you get the CodeComplete working. The solution is to store information about types and functions, probably in some binary form (or XML?) in a separate file, generated by the compiler when it processes the module. So, for each module, you get an .obj with the code in it, and, say, a .dmi (D module interface) with all public members listed. Then, both the compiler and the IDE can rely on information in this file.
Apr 06 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8n8h9$1dh2$1 digitaldaemon.com...
 "OddesE" <OddesE_XYZ hotmail.com> wrote in message
 news:a8n4sq$19ub$1 digitaldaemon.com...

 I wouldn't call class completion, no matter how much I
 like it, the best feature of Delphi...  :)

Why not?

Because I like it's drag-and-drop form editing capabilities and the object inspector etc. more :) This is what sets Delphi apart from the crowd. Visual Studio also has code completion (although it's a little buggy), but it's drag and drop form editing comes nowhere near that of Delphi! It's improved (read: More like Delphi) in .net though! ;) But I *love* code completion too!
 By the way, Delphi is just an example of such approach - there are no
 headers there, and units can be in compiled form - and still you
 get the CodeComplete working. The solution is to store information
 about types and functions, probably in some binary form (or XML?) in a
 separate
 file, generated by the compiler when it processes the module. So,
 for each module, you get an .obj with the code in it, and, say,
 a .dmi (D module interface) with all public members listed. Then,
 both the compiler and the IDE can rely on information in this file.

You are right. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Apr 07 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:a8phk0$1o23$1 digitaldaemon.com...
 Because I like it's drag-and-drop form editing
 capabilities and the object inspector etc. more  :)
 This is what sets Delphi apart from the crowd.
 Visual Studio also has code completion (although it's
 a little buggy), but it's drag and drop form editing
 comes nowhere near that of Delphi! It's improved
 (read: More like Delphi) in .net though!  ;)

Drag'n'drop rulez. Code completion rulez too. D&D + CodeCompletion = Delphi (or C++Builder for us C++ fans). Conclusion: Delphi (C++Builder) rulez.
Apr 07 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8po2b$25nj$1 digitaldaemon.com...
 "OddesE" <OddesE_XYZ hotmail.com> wrote in message
 news:a8phk0$1o23$1 digitaldaemon.com...
 Because I like it's drag-and-drop form editing
 capabilities and the object inspector etc. more  :)
 This is what sets Delphi apart from the crowd.
 Visual Studio also has code completion (although it's
 a little buggy), but it's drag and drop form editing
 comes nowhere near that of Delphi! It's improved
 (read: More like Delphi) in .net though!  ;)

Drag'n'drop rulez. Code completion rulez too. D&D + CodeCompletion = Delphi (or C++Builder for us C++ fans). Conclusion: Delphi (C++Builder) rulez.

Yep! :) On that note, is there something like the Delphi 'published' keyword in D? For people not used to programming in Delphi: In addition to private, protected and public Delphi introduces the keyword published to declare that a variable may be edited from the object inspector at design-time. This is a very cool feature of Delphi IMHO. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Apr 07 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:a8q0sk$2k82$1 digitaldaemon.com...

 On that note, is there something like the Delphi
 'published' keyword in D?

No. But I was thinking of asking Walter to add it. =)
 For people not used to programming in Delphi:
 In addition to private, protected and public
 Delphi introduces the keyword published
 to declare that a variable may be edited from
 the object inspector at design-time. This is
 a very cool feature of Delphi IMHO.

Well, actually its meaning is a bit different - it said that the member - function or variable - must be included into the RTTI members table for that class. Since it doesn't make sence to include all members of the class into the table, thus bloating the executable heavily, you just mark those variables as "accessable via RTTI" explicitly. And then Object Inspector looks at the table of members of the class, and displays those found there...
Apr 07 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8q2gf$2lt9$1 digitaldaemon.com...
 "OddesE" <OddesE_XYZ hotmail.com> wrote in message
 news:a8q0sk$2k82$1 digitaldaemon.com...

 On that note, is there something like the Delphi
 'published' keyword in D?

No. But I was thinking of asking Walter to add it. =)

:)
 For people not used to programming in Delphi:
 In addition to private, protected and public
 Delphi introduces the keyword published
 to declare that a variable may be edited from
 the object inspector at design-time. This is
 a very cool feature of Delphi IMHO.

Well, actually its meaning is a bit different - it said that the member - function or variable - must be included into the RTTI members table for that class. Since it doesn't make sence to include all members of the class into the table, thus bloating the executable heavily, you just mark those variables as "accessable via RTTI" explicitly. And then Object Inspector looks at the table of members of the class, and displays those found there...

Ah ok, thanks for explaining. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Apr 07 2002