What would be the D equivalent of the factory pattern? This obviously doesn't work:

struct A {
  int x = 42;
}

struct B {
  int x = 7;
}

auto factory(string type) {
  if (type == "A")
    return A();
  else if (type == "B")
    return B();
  else
    return A();  // default
}

void main()
{
  auto a = factory("A");
}

Error: mismatched function return type inference of B and A

(dmd 2.067.1)

Reply via email to