Logo

Programming-Idioms

History of Idiom 73 > diff from v393 to v394

Edit summary for version 394 by programming-idioms.org:
[D] DPaste has been down for a while ✝

Version 393

2019-09-29, 08:54:51

Version 394

2020-05-21, 22:06:27

Idiom #73 Create a factory

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

Idiom #73 Create a factory

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

Variables
fact,str
Code
auto fact(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
https://dpaste.dzfl.pl/221d6b58bdc6