Search code examples
d

Template interface containing template struct


Given a struct template, and a template interface that has a member that returns the struct template as well as another member of a different type, what is the correct way to design and implement?

I've the following, but I'm getting compile errors on implementation:

struct TemplateStruct(T, U) {
  T a;
  U b;
}

interface IFoo(V) {
  TemplateStruct resulta();
  V resultb();
}

class Foo : IFoo!int {
  TemplateStruct!(bool, string) resulta() {
    return TemplateStruct!(bool, string)(true, "a");
  }

  int resultb() {
    return 1;
  }
}

Looking through the online Programming in D book doesn't cover implementing this sort of situation.


Solution

  • Here is your mistake:

    interface IFoo(V) {
        TemplateStruct resulta(); // <== Here
        V resultb();
    }
    

    resulta() returns TemplateStruct, which is a template, not a full type. You'll need to specify its template parameters, like you do in class Foo:

    interface IFoo(V) {
        TemplateStruct!(bool, string) resulta();
        V resultb();
    }