Create Collections from Tlist and TObjectList

Issue #309 resolved
Alexandre Santanna created an issue

Today is possible to create a Collection from an Array, so I can do that:

var SpringList : IList<TPessoa>; DelphiList : TList<TPessoa>; begin DelphiList := TList<TPessoa>.create; DelphiList.Add(TPessoa.create(1, 'ALEXANDRE'));

SpringList := TCollections.CreateList<TPessoa>(DelphiList.ToArray);

Why not create directly from TList?

SpringList := TCollections.CreateList<TPessoa>(DelphiList);

Comments (4)

  1. Stefan Glienke repo owner
    • changed status to open

    I consider adding some overloads to an extension (helper) for TCollections for a next version.

    However not on the TCollections from Spring.Collections because of some issue on how the compiler treats overloads of generic methods. That means it produces every overload to a method being called into the dcu including any types - see https://quality.embarcadero.com/browse/RSP-18080

    That is why in 1.3 I will remove any dependencies on System.Generics.Collections from any Spring.Collections unit. In a current develop version this already reduces binary bloat and compile time significantly.

  2. Stefan Glienke repo owner

    Here is some example code how to achieve this because currently I am hesitating to add such methods into a new unit and make it complete for all eligible types - so feel free to make your own interop helper to transfer data from existing RTL collections into Spring4D ones:

    type
      TCollectionsHelper = class helper for Spring.Collections.TCollections
        class function CreateList<T>(const source: TEnumerable<T>): IList<T>; overload; static;
      end;
    
    class function TCollectionsHelper.CreateList<T>(
      const source: TEnumerable<T>): IList<T>;
    var
      item: T;
    begin
      Result := TCollections.CreateList<T>;
      for item in source do
        Result.Add(item);
    end;
    
  3. Log in to comment