On Monday, 28 May 2018 at 09:59:50 UTC, DigitalDesigns wrote:
Implementing interfaces can be a pain but are necessary.

I like to use abstract classes and provide a base implementation. It would be cool if I could use D's awesome meta features to extract the interface from the abstract class then build it. This requires some funky stuff which I'm not sure D can do

Creating an interface based on a class is no big deal in D:

interface Interface(T)
{
    import std.traits;
    static foreach (fn; __traits(allMembers, T))
        static foreach (overload; MemberFunctionsTuple!(T, fn))
mixin("ReturnType!overload "~fn~"(Parameters!overload);");
}

class A
{
   void fun() {}
}

alias IA = Interface!A;

However, you run into a problem when A is expected to implement IA: in order to figure out which functions should be in the interface, we need to know which functions are in A. And in order to figure out which functions are in A, we need to know which functions are in IA. It's a loop with no real solution.

In this specific case, one could consider only members of A, ignoring any interfaces or base classes. This isn't currently possible, but it's not an entirely unreasonable enhancement request.

--
  Simen

Reply via email to