We're having some problems registering generic classes to factories in Delphi.

We're having some problems registering generic classes to factories in Delphi.

Assume I have a parameterized class MyClass with interface MyInterface.

I wish to implement a factory that has a GetInstance(const AType: string) method, which should return a MyInterface, with implementing class of course depending on the string.

It also has a Register(AFunc: TFunc; AType: string) method, which should allow me to extend the factory.

This has proven a rather difficult task, as we wish to implement a standard object to return, only depending on T. But for T = double, we want a different default implementation.

The thing is, we don't want to hardcode these defaults into the factory. We want to be able to register them and allow them to be overwritten by the user, should they wish to do so.

Our current strategy is to have the AType argument default to '', which makes it fairly straightforward to implement defaults, but that of course does not allow us to call Register('something'), as T is an "unknown" abstract type at compile time.

It's fairly easy to make factories for non-generics, but this has proven to be rather difficult for us.

Comments