Re: Factory pattern in D

2015-05-01 Thread biozic via Digitalmars-d-learn
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

Re: Factory pattern in D

2015-05-01 Thread Rikki Cattermole via Digitalmars-d-learn
On 1/05/2015 10:01 p.m., Chris wrote: 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

Re: Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
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

Re: Factory pattern in D

2015-05-01 Thread Namespace via Digitalmars-d-learn
How about this: struct A { int x = 42; } struct B { int x = 7; } T factory(T)() { return T(); } void main() { auto a = factory!(A); }

Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
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() {

Re: Factory pattern in D

2015-05-01 Thread Namespace via Digitalmars-d-learn
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); } Of course, you can restrict the type to A or B, or both: T factory(T)() if

Re: Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
On Friday, 1 May 2015 at 10:46:20 UTC, Chris wrote: On Friday, 1 May 2015 at 10:27:16 UTC, biozic wrote: 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

Re: Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
On Friday, 1 May 2015 at 10:27:16 UTC, biozic wrote: 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 =

Re: Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
On Friday, 1 May 2015 at 10:47:22 UTC, Chris wrote: On Friday, 1 May 2015 at 10:46:20 UTC, Chris wrote: On Friday, 1 May 2015 at 10:27:16 UTC, biozic wrote: 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:

Re: Factory pattern in D

2015-05-01 Thread biozic via Digitalmars-d-learn
On Friday, 1 May 2015 at 11:01:29 UTC, Chris wrote: On Friday, 1 May 2015 at 10:47:22 UTC, Chris wrote: On Friday, 1 May 2015 at 10:46:20 UTC, Chris wrote: On Friday, 1 May 2015 at 10:27:16 UTC, biozic wrote: On Friday, 1 May 2015 at 10:12:36 UTC, Chris wrote: On Friday, 1 May 2015 at

Re: Factory pattern in D

2015-05-01 Thread anonymous via Digitalmars-d-learn
On Friday, 1 May 2015 at 11:01:29 UTC, Chris wrote: This aside, how would I get something to load dynamically? It's either mismatched function return type or (with type check) variable X cannot be read at compile time: void main(string[] args) { auto type = args[1]; auto myType =

Re: Factory pattern in D

2015-05-01 Thread Chris via Digitalmars-d-learn
On Friday, 1 May 2015 at 11:11:28 UTC, biozic wrote: On Friday, 1 May 2015 at 11:01:29 UTC, Chris wrote: Thinking about it, T factory(T)() { return T(); } is better suited for a factory (with static type checks). But then I don't know what factory!X() provides that X() alone doesn't.

Re: Factory pattern in D

2015-05-01 Thread biozic via Digitalmars-d-learn
On Friday, 1 May 2015 at 11:20:32 UTC, Chris wrote: On Friday, 1 May 2015 at 11:11:28 UTC, biozic wrote: On Friday, 1 May 2015 at 11:01:29 UTC, Chris wrote: Thinking about it, T factory(T)() { return T(); } is better suited for a factory (with static type checks). But then I don't know