Factory pattern in D

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 1 04:01:28 PDT 2015


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:
>>>>>
>>>>> ----
>>>>> 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;
>>> }
>>> ---
>>
>> Duh, I tried `static if` yesterday and I still got "Error: 
>> mismatched function return type inference of B and A". Now it 
>> works! Sure, I must have overlooked something. Sometimes it's 
>> better to go home and call it a day than to try to get 
>> something to work.
>
> And thanks, by the way!

Thinking about it,

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

is better suited for a factory (with static type checks).

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 = factory!type();
}

So it's back to classes/interfaces again? Hmmmm.


More information about the Digitalmars-d-learn mailing list