Interfaces Delphi and IList <T> (or TObjectList <T>)

I am trying to implement Spring 4 Delphi and only a program for interfaces instead of classes. However, this is not possible if you want to use a TObjectList.

Consider the following code:

unit Unit1;

interface

uses
  Spring.Collections,
  Spring.Collections.Lists;

type

  IMyObjParent = interface
  ['{E063AD44-B7F1-443C-B9FE-AEB7395B39DE}']
    procedure ParentDoSomething;
  end;

  IMyObjChild = interface(IMyObjParent)
  ['{E063AD44-B7F1-443C-B9FE-AEB7395B39DE}']
    procedure ChildDoSomething;
  end;

implementation

type
  TMyObjChild = class(TInterfacedObject, IMyObjChild)
  protected
    procedure ParentDoSomething;
  public
    procedure ChildDoSomething;
  end;


{ TMyObj }

procedure TMyObjChild.ChildDoSomething;
begin

end;

procedure TMyObjChild.ParentDoSomething;
begin

end;

procedure TestIt;
var
  LMyList: IList<IMyObjChild>;
begin
  TCollections.CreateObjectList<IMyObjChild>;
  //[DCC Error] Unit1.pas(53): E2511 Type parameter 'T' must be a class type
end;

end.

I know that I can change IMyObjChild to TMyObjChild in the above example, but if I need it in another module or form, how can I do this?

Trying to program only interfaces seems too complicated or impossible once you need a TObjectList.

Grrr ... Any ideas or help?

+4
source share
1 answer

CreateObjectList has a general limitation that its type parameter is a class:

function CreateObjectList<T: class>(...): IList<T>;

, . , . TObjectList Spring.Collections.Lists, , , . CreateObjectList TObjectList<T>, .

TObjectList<T> , OwnsObjects. , Delphi RTL . , , . CreateList. TList<T> - , , IList<T>.

LMyList := TCollections.CreateList<IMyObjChild>;
+4
source

Source: https://habr.com/ru/post/1533662/


All Articles