Default Implementation For an Interface

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 16 11:05:40 PST 2012


On Thursday, February 16, 2012 13:26:59 Jacob Carlborg wrote:
> Since D have delegates I would use those for event handling and not
> listeners. I think they are a much better fit, as long as you don't have
> to force the user to handle many different events on the same object.

Oh, I'm not necessarily arguing that using interfaces far listeners is the way 
to go (in fact, I agree that delegates would be much better). It's just that 
that's a prime example of a situation where you want default implementations 
for interface methods, since with single inheritance, you can't derive a class 
from multiple classes which give you default implementations for interface 
methods.

> > Now, if you use template mixins, I believe that it's possible to use that
> > to mixin default implementations for the functions in an interface, which
> > should solve the problem for D. So, that's probably good enough for D
> > without having to make it so that interface functions can have default
> > implementations.
> 
> Template mixins cause their own problems. You can't overload methods
> with template mixins, may it's possible to get around that with aliases,
> I don't remember.

I thought that you could, since they can be virtual, unlike templated 
functions. I don't know though. It's not something that I've really had to 
worry about - particularly since so few of my D programs need classes, let 
alone interfaces (because most of my D programs are small).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list