www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - Templated Inheritance

↑ ↓ ← Jesse Phillips <jessekphillips gmail.com> writes:
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.
Mar 02 2008
↑ ↓ → Christopher Wright <dhasenan gmail.com> writes:
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.
Mar 02 2008