Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
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, } }
class Parent { constructor(str) {} fact(new_class, str) { if (new_class.prototype instanceof Parent) { return new new_class(str) } } } class Child extends Parent {}
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