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);
end;

type ClassOfParent = class of Parent;

function fact(ClassType: ClassOfParent; const str: string): Parent;
begin
  result := ClassType.Create(str);
end;
def fact(a_class, str_):
    if issubclass(a_class, Parent):
        return a_class(str_)
def fact(klass, str)
  klass.new(str) if klass.is_a?(Parent)
end

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

Idiom created by

bbtemp