[phobos] AutoImplement: Shouldn't it be a mixin template?

David Simcha dsimcha at gmail.com
Tue Sep 7 20:54:29 PDT 2010


  The AutoImplement functionality in std.typecons is currently a class.  
This severely and unnecessarily limits its usefulness in the following 
cases:

1.  You want to implement more than one interface automagically.

2.  You want to implement stuff automatically, but also add more to the 
class.  For example, maybe you want to add some members to the abstract 
base class before auto implementing, and maybe override some of the auto 
implemented functionality.  Of course you could define 3 levels of 
inheritance (the abstract class with all the extra members, the auto 
implemented stuff, and the class with stuff overridden) but this is 
horribly verbose.

3.  You want to evaluate the automatically implemented functionality in 
some scope other than std.typecons, so you can import stuff.

Shouldn't we just make AutoImplement a mixin template instead?  This can 
be done trivially by mixing the string mixin currently used to implement 
it into a mixin template's scope.  It seems like it would solve all the 
relevant problems.  Then we could do stuff like:

interface Interface1 {
     void fun();
     void gun();
}

interface Interface2 {
     void hun();
}

class Class : Interface1, Interface2 {
     mixin AutoImplement!(Interface1, generateEmptyFunction, 
isAbstractFunction);
     mixin AutoImplement!(Interface2, generateEmptyFunction, 
isAbstractFunction);

     // Override the automatic implementation of fun() taking advantage of
     // template mixin name lookup rules.
    override void fun() {
        writeln("Overridden.");
     }
}

The killer use case for this would be automatic forwarding for the 
decorator pattern.  You could just mix in an AutoImplement that forwards 
all of your decorator interface functions to the encapsulated object, 
and then override whichever ones you need to.


More information about the phobos mailing list