Thread overview
Derive interface from class?
Jun 02, 2016
Pie?
Jun 02, 2016
Pie?
Jun 02, 2016
Kagamin
June 02, 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?







June 02, 2016
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?



June 02, 2016
You can see how AutoImplement works for example https://github.com/dlang/phobos/blob/master/std/typecons.d#L3269