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.

Reply via email to