Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <code>TCustomerList&lt;TCustomer&gt;</code> and a <code>TPersonList&lt;TPerson&gt;</code>. The beauty of generics is that you can use the same list for different element types. <em>That means that list and element type must not have any dependencies.</em></p> <p>You can do something like:</p> <pre><code>procedure TSomething.ProcessList&lt;T: TBaseObject&gt;(const aList: TBaseList&lt;T&gt;); begin // process the list using code that is independent of the actual type of T. end; ... var aCustomerList: TBaseList&lt;TCustomer&gt;; aPersonList: TBaseList&lt;TPerson&gt;; begin ProcessList(aCustomerList); ProcessList(aPersonList); </code></pre> <p>Perhaps you may have to specify <code>T</code> (some early versions of generics did not handle type inference -- i.e. that it inferes the type of <code>T</code> from the type of the parameter -- very well), i.e.</p> <pre><code> ProcessList&lt;TCustomer&gt;(aCustomerList); ProcessList&lt;TPerson&gt;(aPersonList); </code></pre> <p>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 <code>aList</code>. And if you really need one, you can only use <code>TObject</code>, but that doesn't allow you to use the list in any useful way. And it is not very generic.</p> <p>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.</p>
 

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