Default Implementation For an Interface

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 17 09:04:59 PST 2012


On Wed, 15 Feb 2012 22:01:51 -0500, Kevin <kevincox.ca at gmail.com> wrote:

> I was implementing a framework and I found that I wanted two things.
>   - A strong set of interfaces so that I can get what I want from a  
> variety of sources.
>   - Some basic implementations of these interfaces.
>
> For example, say I was writing a database class.  I could either name  
> the interface Database and call the class DatabaseImplementation or  
> something but that is ugly.  If I call the interface IDatabase, the  
> Database class looks nice but I need to convince users to write  
> functions that take IDatabases not Databases.
>
> I was wondering if there was any way to implement a default  
> implementation.  This way, I could create my Database interface and  
> classes could implement that but if you called `new Database()` you  
> would still get a basic database.

Aside from what has been said already, if you wish to have methods that  
are not static defined in the interface, final methods currently work:

interface I
{
     void foo();
     final void callFoo() {writeln("about to call foo"); foo();  
writeln("ok, I called foo");}
}

This isn't exactly a "default implementation", since you can't override it.

Note that template methods are supposed to work (And also are implicitly  
final), but this doesn't currently work.

http://d.puremagic.com/issues/show_bug.cgi?id=4174

-Steve


More information about the Digitalmars-d-learn mailing list