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

  • Home
  • SEARCH
  • 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 7420873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:18:51+00:00 2026-05-29T08:18:51+00:00

I know the title is Very wide – spanning over a lot ! And

  • 0

I know the title is Very wide – spanning over a lot !

And I hope that this question might evolve to a bigger “info wiki thingy” on the subjects.

What I have learned – so far:

  • When using Generics – understand the concepts (covariance and contravariance).
  • Do NOT “mis-use” the concept of generics combined with inheritance. I did and it could make you head directly into covariance problems! Make sure you “break off” the generic at the correct point in you inheritance – if you are combining the two.

(please correct me – if you think i’m wrong, missing or have misunderstood anything).

My problem was:

But by now I’ve spend countless hours, trying to figure out, how to solve this “big puzzle” I have on my desk. And I’ve gotten some good answers from several of you SO users already – but now its time to get something working in a bigger scale.

I ventured into Generics with this one:
Generics and Polymorphism working together

And now I’m kinda stuck on this one:
Situations where Generics won’t work

Why I end up with covariance problems – is because of my class procedure in my hierarchy.

So I’m wondering if Interfaces is my next bold move in this “saga”.
How do one “step over” a covariance problem.
One thing is to find out that you actually have this problem – another thing is “how to work around it”.

So IF any of you good people “out there” has any opinions on this – I’m all ears.
Basically :
Tell me to go for Interfaces (I have never done one from scratch myself).
Or .. throw me a bone in the direction you would suggest.

My current source pool is as stated in the second link – from the top.

Here is a small snippet from my earlier post that shows my covariance problem.
David kindly explained – Why I ran into the bush.. But now I need info on – How to run around it.

var    
  aList : TBaseList<TBaseObject>;  // used as a list parameter for methods
  aPersonList : TPersonList<TPerson>;
  aCustomerList : TCustomerList<TCustomer>;
begin
  aPersonList := TPersonList<TPerson>.Create;
  aCustomerList := TCustomerList<TCustomer>.Create;

  aList := aCustomerList;  <-- this FAILS !!  types not equal ..

end;

Regards

  • 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-05-29T08:18:52+00:00Added an answer on May 29, 2026 at 8:18 am

    You can’t do what you want to do, but that is not how you use generics anyway. As Rob Kennedy said, it makes no sense to have a TCustomerList<TCustomer> and a TPersonList<TPerson>. The beauty of generics is that you can use the same list for different element types. That means that list and element type must not have any dependencies.

    You can do something like:

    procedure TSomething.ProcessList<T: TBaseObject>(const aList: TBaseList<T>);
    begin
      // process the list using code that is independent of the actual type of T.
    end;
    
    ...
    
    var
      aCustomerList: TBaseList<TCustomer>;
      aPersonList: TBaseList<TPerson>;
    begin
      ProcessList(aCustomerList);
      ProcessList(aPersonList);
    

    Perhaps you may have to specify T (some early versions of generics did not handle type inference — i.e. that it inferes the type of T from the type of the parameter — very well), i.e.

      ProcessList<TCustomer>(aCustomerList);
      ProcessList<TPerson>(aPersonList);
    

    But that, or something similar, is what you should do. Anything else doesn’t make sense, IMO. There is no need to have a variable that could hold any of these lists, like your aList. And if you really need one, you can only use TObject, but that doesn’t allow you to use the list in any useful way. And it is not very generic.

    Interfaces won’t help you at all with this problem. You can give classes certain capabilities, i.e. also the elements of the lists, through interfaces (another kind of polymorphism). But that won’t handle covariance.

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

Sidebar

Related Questions

The title to this question isn't written very well. Sorry 'bout that. I'm curious
I know the title isn't very clear. I'm new to PHP, so there might
I don't know which title I should use for this question. I have a
I know that question with same title has been asked almost 6 month ago.
I know there are other questions that are very similar to this, but their
I know that the title is very subtle but I have absolutely no idea
First off, I'm not entirely sure that my question title is very descriptive, so
Folks, I know I didn't phrase that title very well, but here's the scenario.
Apologies for the very broadly title question. Basically this follows on from my earlier
I know the title isn't very elaborate, but I have tried this multiple times

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.