Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Delphi generics do not support covariance and contravariance so what you are attempting to do is not possible with the language's current syntax. I suggest you have a read of the following blog articles that cover the matter in more detail.</p> <ul> <li><a href="http://blogs.teamb.com/craigstuntz/2009/10/01/38465/" rel="nofollow noreferrer">Craig Stuntz: Comparing C#, C++, and Delphi (Win32) Generics</a></li> <li><a href="http://tech.turbu-rpg.com/149/generics-and-the-covariance-problem" rel="nofollow noreferrer">Mason Wheeler: Generics and the Covariance Problem</a></li> </ul> <p>Fundamentally what you are attempting to do is this:</p> <pre><code>type TBase = class; TDerived = class(TBase); TBaseList = TList&lt;TBase&gt;; TDerivedList = TList&lt;TDerived&gt;; var BaseList: TBaseList; DerivedList: TDerivedList; ... BaseList := TDerivedList;//does not compile </code></pre> <p>The designers have not stopped you doing this out of spite. There is a good reason. Consider the following standard example:</p> <pre><code>type TAnimal = class; TCat = class(TAnimal); TPenguin = class(TAnimal); var AnimalList: TList&lt;TAnimal&gt;; CatList: TList&lt;TCat&gt;; Penguin: TPenguin; ... AnimalList := CatList;//does not compile because... AnimalList.Add(Penguin);//...of the danger of this </code></pre> <p>Whilst it is reasonable to add a <code>TPenguin</code> to a <code>TList&lt;TAnimal&gt;</code>, the actual list that <code>AnimalList</code> refers to is a <code>TList&lt;TCat&gt;</code> and a penguin is not a cat.</p> <p>And, if you want to think of it in the context of your example hierarchy, here's an illustration of code that justifies the language design.</p> <pre><code>aList := aCustomerList;//does not compile aList.Add(aCustomPerson); //this would add a TCustomPerson instance to a list containing //TCustomer instances, but a TCustomPerson is not a TCustomer </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload