www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - another meta programming idea

I am thinking of an interface like this:

: interface DataWalker {
:    int walkData(char[] classname, char[][] fields, ...);
: }

The concept is that the user could define a class, and define a DataWalker
sub-class, and then apply the data walker to the class.

When they do so, they get a call to the interface above, and they can use
variadic type inspection on the fields of the struct / class.

: class OrdinaryPOD {
:    int x; int y; char[] z;
:   private:
:    double zig;
: }
: class PickleWalker : public DataWalker {
:    int walkData(char[] name, char[][] fields, ...)
:    {
:       ...
:    }
: }
: OrdinaryPOD pod1 = new OrdinaryPOD;
: PickleWalker walk1 = new OrdinaryPOD;
: // same as: walk1.walkData("OrdinaryPOD", ["x", "y", "z", "zig"],
:                            pod1.x, pod1.y, pod1.z, pod1.zig);
: pod1.walkPublic(walk1);
: // same as: walk1.walkData("OrdinaryPOD", ["x", "y", "z"],
: //                         pod1.x, pod1.y, pod1.z, pod1.zig);
: pod1.walkPrivate(walk1);

Note that the "same as" call is not completely accurate: the 'private' version
of this mechanism (if supported) would not be hampered by access control.

Also, "property" like (implicit function call) members could be returned by this
technique.  They could be returned as "x()" instead of "x".

I am thinking this would work like template instantiation; when the user needs
this they would need to declare somewhere (usually *outside* of the inspected
class) that the class data is required.

(I'm not sure what walkData should return (if anything) in the int.)

May 15 2005