www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13830] New: Circular dependency between interface and

https://issues.dlang.org/show_bug.cgi?id=13830

          Issue ID: 13830
           Summary: Circular dependency between interface and
                    implementation segfault the compiler
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

I tried to use the CRTP in D to generate an interface at compile time based on
another interface and the implementation. Unfortunately, it segfaulted the
compiler.

````
import std.stdio, std.traits;

template RESTServer(I, Impl) {
        private abstract class IRESTServer/* : I*/ {
                //final override void test() { assert(0); }                     
                void test(Context ctx);
        }
        int abuseTheCompiler() {
                foreach (method; __traits(allMembers, Impl)) {
                        foreach (overload; MemberFunctionsTuple!(Impl, method))
{
                                pragma(msg, __traits(identifier, overload));
                        }
                }
                return 42;
        }
        static assert(abuseTheCompiler());
        alias RESTServer = IRESTServer;
}

struct Context { string ip; }

interface MyAPI { void test(); }

class MyAPIImpl : RESTServer!(MyAPI, MyAPIImpl) {
override:
        void test(Context ctx) {
                writeln(ctx.ip);
        }
}

void main() {
        auto c = new MyAPIImpl();
        c.test(Context("10.24.50.42"));
}
````

Question: should such code be valid ? It allows an interesting pattern for code
generators.

--
Dec 07 2014