Templated Inheritance

Christopher Wright dhasenan at gmail.com
Sun Mar 2 20:10:31 PST 2008


Jesse Phillips wrote:
> Templates, traits, mixins, and meta programming I haven't really gotten 
> into. But I came to note that templating inheritance of a class works 
> great.
> 
> http://dsource.org/projects/tutorials/wiki/TemplatedInheritanceExample
> 
> My question is what others think about the possibility of using traits 
> from D2.0 to allow for interface instantiation without reimplementing 
> methods? I don't know how practical it would be but it might be quite 
> interesting.

I have an example, and a usable one, that basically proxies a class or 
interface using templated inheritance. It doesn't quite support concrete 
classes right now, because I want a reliable solution, and a concrete 
class could define a constructor that throws an exception or 
dereferences a null pointer or some such. All the virtual methods for 
the concrete class will be properly proxied; it's just that you depend 
on the constructor to work correctly.

The code's here:
http://www.dsource.org/projects/dmocks/browser/trunk/dmocks/dmocks/MethodMock.d
http://www.dsource.org/projects/dmocks/browser/trunk/dmocks/dmocks/MockObject.d

Not really that difficult. How it applies to your question isn't quite 
clear, though; you want your interface methods to do something useful. 
The typical tactic with that is to define a mixin with default 
implementations. Works best if you do that with an abstract base class 
and then override whatever you need to, I think.


More information about the Digitalmars-d-learn mailing list