On Friday, 1 May 2015 at 10:12:36 UTC, Chris wrote:
On Friday, 1 May 2015 at 10:04:46 UTC, Namespace wrote:
How about this:

----
struct A {
 int x = 42;
}

struct B {
 int x = 7;
}

T factory(T)() {
 return T();
}

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

That's what I was looking for, I just couldn't get it right. Thanks.

Rikki:

I wanted to avoid classes and interfaces.

This might be a bit more useful:
---
struct A {
  int x = 42;
}

struct B {
  int x = 7;
}

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

void main()
{
  auto a = factory!"A";
  auto b = factory!"B";
  auto x = factory;
}
---


Reply via email to