This language bar is your friend. Select your favorite languages!

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.

auto fact(T, A...)(A a)
if (is(T==class) && is(T: Parent))
    return new T(a);
type ParentFactory func(string) Parent

var fact ParentFactory = func(str string) Parent {
	return Parent{
		name: str,
type Parent = class
  constructor create(const str: string);

type ClassOfParent = class of Parent;

function fact(ClassType: ClassOfParent; const str: string): Parent;
  result := ClassType.Create(str);
use Safe::Isa;
sub fact {
    my ($class, $str) = @_;
    return $class->new($str) if $class->$_isa('Parent');
def fact(a_class, str_):
    if issubclass(a_class, Parent):
        return a_class(str_)
def fact(klass, str) if klass.is_a?(Parent)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by