Enhancement request

Sjoerd van Leent svanleent at gmail.com
Thu Jul 9 04:29:05 PDT 2009


Robert Clipsham Wrote:

> Sjoerd van Leent wrote:
> > As D is typically a system language, this enhancement might not make it, but I still think it would be practical.
> > 
> > I've been thinking of a real world example for this, and yesterday found a nice example to illustrate.
> > 
> > Suppose you would be creating an API to interrogate a database. Now within the database, there is a table with some records. Suppose you would like to represent this table as a D class and a record as an instance of this class (or a struct, whatever).
> > 
> > class Person
> > {
> >     uint id;
> >     string first_name;
> >     string last_name;
> >     uint age;
> > }
> > 
> > Now, for the API to know how to use this class, it must most likely be inheriting some kind of interface and some mapping to actually have the member variables linked to actual columns within the table.
> > 
> > But it also would make the class look big and fat. So instead of that solution, might it not be better to have a way to easily get to the member names. I was thinking of a keyword in front of a type specification (class, struct, enum, etc.). For example, the keyword "reflectable". (or a pragma)
> > 
> > reflectable class Person
> > {
> >     uint id;
> >     string first_name;
> >     string last_name;
> >     uint age;
> > }
> > 
> > Now the compiler knows that it has to do some magic stuff to make the fields accessible using strings etc.
> > 
> > I know that it might be possible to do all of this through the vtable, classinfo, typeinfo and so on, but it doesn't appear to be very straightforward.
> 
> http://codepad.org/1pZN2RSD
> 
> Is that what you want? That gives you the types and names of all the 
> properties of a struct/class. If you then want to interface with a 
> database with the struct or class being automatically filled out you 
> could use some template magic:
> ----
> import std.string : rfind;
> 
> struct Person
> {
>    uint id;
>    string first_name;
>    string last_name;
>    uint age;
> }
> 
> T getFromDb(T)(string field)
> {
>    // Code to get values from the database
> }
> 
> void get( T )( ref T s )
> {
>    foreach(k, v; s.tupleof)
>    {
>      // Used to strip off (Person ). from the property name
>      auto ldot = rfind( T.tupleof[k].stringof, "." ) + 1;
>      s.tupleof[k] = getFromDb!(typeof(v))
>                     (T.tupleof[k].stringof[ldot..$ ]);
>    }
> }
> 
> void main()
> {
>    Person p;
>    get( p ); // P is now populated with the values from the db
> }
> ----


Now suppose that get() is somewhere in a library that isn't aware of object Person and of a template. Because that is the problem with libraries and especially DLL's. It needs to be more dynamic. Theoretically, it should be possible to extract everything out of Person without knowing any of the particular behavior of that class. So accessing fields, writing to fields, etc. should be possible with such libraries.

This has allways been my objection towards the heavy usage of templates. If somehow templates could be instantiated within the runtime of a program...



More information about the Digitalmars-d mailing list