title: What's the intended way to pass a list?
body: <p>I have an existing class, that has an existing method, that allows you to pass it a list of <em>stuff</em>:</p>
  
  <pre><code>TContoso = class(TSkyrim)
  public
     procedure AddObjects(Objects: TList);
  end;
  </code></pre>
  
  <p>And so, in the before-times, someone could pass a <code>TList</code> or a <code>TObjectList</code> to the method:</p>
  
  <pre><code>var
     list: TList;
  
  list := TObjectList.Create(True);
  contoso.AddObjects(list);
  </code></pre>
  
  <p>It didn't matter, as <code>TObjectList</code> was a <code>TList</code>. My method was flexible; it could take either.</p>
  
  <h2>Now in the after times</h2>
  
  <p>Now in the after times, i prefer typed lists:</p>
  
  <pre><code>var
     list: TList&lt;TGrobber&gt;;
  
  list := TObjectList&lt;TGrobber&gt;.Create(True);
  contoso.AddObjects(list);
  </code></pre>
  
  <p>Of course that doesn't compile, as neither <code>TList&lt;T&gt;</code> nor <code>TObjectList&lt;T&gt;</code> descend from <code>TList</code>. Which isn't such a problem. I intuitiavely understand that i don't actually need a <code>TList</code>, i just need something that is <em>"enumerable"</em>:</p>
  
  <p>Based on my experience in the .NET FCL, that means i simply need to declare the parameter is <code>IEnumerable</code>, because <strong>everything</strong> is enumerable:</p>
  
  <ul>
  <li><code>IEnumerable&lt;T&gt;</code> comes from <code>IEnumerable</code></li>
  <li><code>ICollection</code> comes from <code>IEnumerable</code></li>
  <li><code>ICollection&lt;T&gt;</code> comes from <code>IEnumerable</code></li>
  <li><code>IList</code> comes from <code>IEnumerable</code></li>
  <li><code>IList&lt;T&gt;</code> comes from <code>IEnumerable</code></li>
  <li><code>List</code> comes from <code>IEnumerable</code></li>
  <li><code>List&lt;T&gt;</code> comes from <code>IEnumerable</code></li>
  </ul>
  
  <p>So i would do something like:</p>
  
  <pre><code>TContoso = class(TSkyrim)
  public
     procedure AddObjects(Objects: IEnumerable);
  end;    
  </code></pre>
  
  <p>Except the Delphi BCL doesn't allow the polymorphism that .NET world allows; the things that are enumerable don't implement the <code>IEnumerable</code> interface:</p>
  
  <pre><code>TList = class(TObject)
  public
     function GetEnumerator: TListEnumerator;
  end;
  
  TObjectList = class(TList);
  
  TList&lt;T&gt; = class(TEnumerable&lt;T&gt;)
  public
     function GetEnumerator: TEnumerator&lt;T&gt;;
  end;
  
  TObjectList&lt;T&gt; = class(TList&lt;T&gt;);
  </code></pre>
  
  <p>Without the typing, how does the compiler know a type is enumerable?</p>
  
  <p><a href="http://docwiki.embarcadero.com/RADStudio/XE7/en/Declarations_and_Statements" rel="noreferrer">Delphi uses secret hard-coded magic.</a>:</p>
  
  <blockquote>
    <p>the class or interface must implement a prescribed collection pattern. A type that implements the collection pattern must have the following attributes:
    - The class or interface must contain a public instance method called <code>GetEnumerator()</code>. The <code>GetEnumerator()</code> method must return a class, interface, or record type.
    The class, interface, or record returned by <code>GetEnumerator()</code> must contain a public instance method called <code>MoveNext()</code>. The <code>MoveNext()</code> method must return a <strong>Boolean</strong>.
    - The class, interface, or record returned by <code>GetEnumerator()</code> must contain a public instance, read-only property called <code>Current</code>. The type of the <code>Current</code> property must be the type contained in the collection.</p>
  </blockquote>
  
  <p>What is the way that the language designers intended me to use enumerables in my code?</p>
  
  <ul>
  <li>What do i declare the type of paramater</li>
  <li>how do i check for the presence of a method called <code>GetEnumerator</code>?</li>
  <li>how do i call the method <code>GetEnumerator</code>?</li>
  <li>how do i call the <code>Current</code> property?</li>
  <li>how do i call the <code>Next</code> method?</li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>TContoso = class(TSkyrim)
  public
     procedure AddObjects(const Objects);
  end;    
  
  procedure TContoso.AddObjects(const Objects);
  var
     o: TObject;
     enumerator: TObject;
     bRes: Boolean;
  begin
     //for o in Objects do
     //   InternalAdd(nil, '', o);
  
     if not HasMethod(Objects, 'GetEnumerator') then
        Exit;
  
      enumerator := InvokeMethod(Objects, 'GetEnumerator');
  
      if not HasMethod(enumerator, 'MoveNext') then
         Exit;
  
      bRes := InvokeMethod(enumerator, 'MoveNext');
  
      while bRes do
      begin
         if HasMethod(enumerator, 'Current');
             InternallAdd(nil, '', InvokeMethod(enumerator, 'Current'));
  
         bRes := InvokeMethod(enumerator, 'MoveNext');
      end;
  end;
  </code></pre>
  
  <p>What is the intended way to pass <em>"an enumerable bag of stuff"</em>?    </p>
  
  <h1>Hack</h1>
  
  <pre><code>TContoso = class(TSkyrim)
  public
     procedure AddObjects(Objects: TList); overload;
     procedure AddObjects(Objects: TList&lt;T&gt;); overload;
  end;    
  </code></pre>
  
  <p>There must be a reason the designers chose not to have <code>IList</code> implement <code>IEnumerable</code>. There must be a compile time mechanism to iterate a list. But what is that reason, and what is that way.</p>