Derive interface from class?

Pie? via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 09:14:47 PDT 2016


Since D uses the single inheritance model for classes, I wonder 
if it is possible to write a class then extract the interface 
from it?

e.g.,

class myclass
{
    public void foo() { writeln("adf"); }
}

mixin!(extractInterface!(myclass, myinterface));


Where the mixin creates the interface as if I typed it by hand:

interface myinterface
{
    void foo();
}

This would greatly simplify having to start writing from a base 
interface then add the class. It would also let one do multiple 
inheritance from classes that are already defined.

Since every class must have a well defined interface, this seems 
like it should be possible and relatively easy using reflection. 
A bad idea?









More information about the Digitalmars-d-learn mailing list