Derive interface from class?

Pie? via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 09:41:36 PDT 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?





More information about the Digitalmars-d-learn mailing list