Logo

Programming-Idioms

History of Idiom 73 > diff from v321 to v322

Edit summary for version 322 by programming-idioms.org:
[D] pFact -> fact

Version 321

2016-06-12, 16:52:09

Version 322

2016-06-12, 16:55:24

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Code
auto pFact(T, A...)(A a)
if (is(T==class) && is(T: Parent))
{
    return new T(a);
}
Code
auto fact(T, A...)(A a)
if (is(T==class) && is(T: Parent))
{
    return new T(a);
}
Comments bubble
In D we can cover all the possible constructors using a variadic argument. Parent is checked statically with a constraint.
Comments bubble
In D we can cover all the possible constructors using a variadic argument. Parent is checked statically with a constraint.
Demo URL
http://dpaste.dzfl.pl/9d4241e06f9e
Demo URL
https://dpaste.dzfl.pl/221d6b58bdc6