Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:53:47+00:00 2026-06-02T19:53:47+00:00

I have problem with my code, which uses generic types. Why doesn’t the compiler

  • 0

I have problem with my code, which uses generic types. Why doesn’t the compiler know that the passed list (Result) is a TObjectList<TItem> (TItem is type for T in TItems)?

Interface:

type
   TItem = class
end;

type
  IItemsLoader = interface
    procedure LoadAll(AList : TObjectList<TItem>);
end;

type
  TItemsLoader = class(TInterfacedObject, IItemsLoader)
public
  procedure LoadAll(AList : TObjectList<TItem>);
end;

type
  IItems<T : TItem> = interface
  function LoadAll : TObjectList<T>;
end;

type
  TItems<T : TItem> = class(TInterfacedObject, IItems<T>)
  private
    FItemsLoader : TItemsLoader;
  public
    constructor Create;
    destructor Destroy; override;
    function LoadAll : TObjectList<T>;
end;

Implementation:

procedure TItemsLoader.LoadAll(AList: TObjectList<TItem>);
begin
  /// some stuff with AList
end;

{ TItems<T> }

constructor TItems<T>.Create;
begin
  FItemsLoader := TItemsLoader.Create;
end;

destructor TItems<T>.Destroy;
begin
  FItemsLoader.Free;
  inherited;
end;

function TItems<T>.LoadAll: TObjectList<T>;
begin
  Result := TObjectList<T>.Create();

  /// Error here
  /// FItemsLoader.LoadAll(Result);
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:53:51+00:00Added an answer on June 2, 2026 at 7:53 pm

    In the function with the error, Result is a TObjectList<T>, where T is some subclass of TItem, but the compiler doesn’t know what specific class it is. The compiler has to compile it so that it’s safe to run for any value of T. That might not be compatible with the argument type of LoadAll, which requires a TObjectList<TItem>, so the compiler rejects the code.

    Suppose T is TItemDescendant, and the compiler allows the faulty code to compile and execute. If LoadAll calls AList.Add(TItem.Create), then AList will end up holding something that isn’t a TItemDescendant, even though it’s a TObjectList<TItemDescendant>. It holds an object of a type different from what its generic type parameter says it holds.

    Just because S is a subtype of T doesn’t mean that X<S> is a subtype of X<T>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code problem which stems from the fact that I am using
I have some fairly generic code which uses preprocessor macros to add a certain
I have problem with querying webservice which uses WS-Security. I use code creadted by
I have made an android app which uses speech recognition. The problem is that
Problem Hello all! I have this code which takes my jpg image loops through
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
I have a container class that has a generic parameter which is constrained to
I have a problem with a C++ application I've developed which uses dlopen to
I have the following piece of code which uses the ALAssets library to store
I have written a piece of code which uses the PdfSharp library. The instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.