When trying the following code it will not compile as I receive this error:
Type parameter 'T' is not compatible with type TBase
I want this type constraint to support the Save method being typed to return the same class as itself.
TTest is not directly a TBase but TBase is the ancestor.
type
TBase = class
end;
TBusiness<T: TBase> = class(TBase)
public
function Save: T; virtual; abstract;
end;
TTest = class(TBusiness<TTest>)
end;
TTestis aTBusiness<TTest>, and aTBusiness<T>is always aTBase. Therefore, aTTestis aTBase, and soTBusiness<TTest>satisfies the constraintTBussiness<T: TBase>. The problem here is that the declaration ofTTestrecursively refers to itself. The secondTTeston the penultimate line refers to theTTestbeing declared at that moment. The compiler doesn't accept this (and it is certainly not a typical use of generics and classes). However, I do understand what the OP wants to achieve; this approach, however, isn't valid.