Is it possible to constrain the type of generic to, say, two different classes?
Like so:
TSomeClass<T: FirstClass; T: SecondClass> = class
// ...
end;
(Sorry about the lack of formatting – the SO tool bar has disappeared from my browser).
I know the above won’t compile, its only written so to give you guys an idea. I tried
TSomeClass<T: FirstClass, SecondClass> = class
// ...
end;
but then I wasn’t allowed to write
procedure TSomeClass.SomeMethod<T> (Param1: string);
Is this even possible?
No, it’s not possible. How should the compiler be able to statically verify that your method calls are valid?
Note, that
is not a valid type constraint. You cannot combine multiple class constraints. You can combine a class constraint with some interface constraints though. But even then
means, that the generic type has to descend from
TSomeClassand implementISomeInterface.So the only thing you can do is to extract the stuff that is common between
FirstClassandSecondClass, put it in an interface and use an interface constraint:Perhaps you can give some more details about what you want to achieve.