Я пытаюсь реализовать функции TCollection
и TCollectionItem
в дженериках. Чтобы решить эту проблему, нам нужно предварительно объявить TGenericCollectionItem
или TGenericCollection
. Использование опережающего объявления универсального типа не поддерживается настоящим (XE6) компилятором Delphi. Есть хак как это сделать. Но у нас все еще есть проблема с назначением владельца TCollectionItem
.
TBaseElement = class // hack for forward declaration of a generic type
end;
TGenericCollection<T: TBaseElement> = class(TObjectList<TBaseElement>)
protected
procedure Notify(const Value: TBaseElement; Action: TCollectionNotification); override;
end;
TGenericCollectionItem = class(TBaseElement)
public
Owner: TGenericCollection<TBaseElement>;
end;
procedure TGenericCollection<T>.Notify(
const Value: TBaseElement; Action: TCollectionNotification);
begin
inherited;
if (Action = cnAdded) then
begin
if (Value is TGenericCollectionItem) then
(Value as TGenericCollectionItem).Owner := Self; //here is error
end;
end;
E2010 Несовместимые типы:
'TGenericCollection<TBaseElement>'
и'TGenericCollection<TGenericCollection<T>.T>'
Как решить этот конфликт?