www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Derive interface from class?

reply Pie? <AmericanPie gmail.com> writes:
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?
Jun 02 2016
next sibling parent Pie? <AmericanPie gmail.com> writes:
On Thursday, 2 June 2016 at 16:14:47 UTC, Pie? wrote:
 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?
I guess the main issue here is that myclass would then need to be specified as being derived from myinterface after it was generated! Not sure if this is possible to solve in D?
Jun 02 2016
prev sibling parent Kagamin <spam here.lot> writes:
You can see how AutoImplement works for example 
https://github.com/dlang/phobos/blob/master/std/typecons.d#L3269
Jun 02 2016