title: Implementing List Enumerator OfType<T> in Delphi
body: <p>I am using Delphi XE to implement an enumerator that allows filtering the elements of the list by type.  I have quickly assembled a test unit as follows:</p>
  
  <pre><code>unit uTestList;
  
  interface
  
  uses Generics.Collections;
  
  type
    TListItemBase = class(TObject)
    end; { TListItemBase }
  
    TListItemChild1 = class(TListItemBase)
    end;
  
    TListItemChild2 = class(TListItemBase)
    end;
  
    TTestList&lt;T : TListItemBase&gt; = class;
  
    TOfTypeEnumerator&lt;T, TFilter&gt; = class(TInterfacedObject, IEnumerator&lt;TFilter&gt;)
    private
      FTestList : TList&lt;T&gt;;
      FIndex : Integer;
    protected
      constructor Create(Owner : TList&lt;T&gt;); overload;
  
      function GetCurrent : TFilter;
      function MoveNext : Boolean;
      procedure Reset;
  
      function IEnumerator&lt;TFilter&gt;.GetCurrent = GetCurrent;
      function IEnumerator&lt;TFilter&gt;.MoveNext = MoveNext;
      procedure IEnumerator&lt;TFilter&gt;.Reset = Reset;
    end;
  
    TOfTypeEnumeratorFactory&lt;T, TFilter&gt; = class(TInterfacedObject, IEnumerable)
    private
      FTestList : TList&lt;T&gt;;
    public
      constructor Create(Owner : TList&lt;T&gt;); overload;
      function GetEnumerator : TOfTypeEnumerator&lt;T, TFilter&gt;;
    end;
  
    TTestList&lt;T : TListItemBase&gt; = class(TList&lt;T&gt;)
    public
      function OfType&lt;TFilter : TListItemBase&gt;() : IEnumerable;
    end; { TTestList }
  
  
  implementation
  
  { TOfTypeEnumerator&lt;T, TFilter&gt; }
  
  constructor TOfTypeEnumerator&lt;T, TFilter&gt;.Create(Owner: TList&lt;T&gt;);
  begin
    inherited;
    FTestList := Owner;
    FIndex := -1;
  end;
  
  function TOfTypeEnumerator&lt;T, TFilter&gt;.GetCurrent: TFilter;
  begin
    Result := TFilter(FTestList[FIndex]);
  end;
  
  function TOfTypeEnumerator&lt;T, TFilter&gt;.MoveNext: Boolean;
  begin
    Inc(FIndex);
    while ((FIndex &lt; FTestList.Count)
           and (not FTestList[FIndex].InheritsFrom(TFilter))) do
    begin
      Inc(FIndex);
    end; { while }
  end;
  
  { TOfTypeEnumeratorFactory&lt;T, TFilter&gt; }
  
  constructor TOfTypeEnumeratorFactory&lt;T, TFilter&gt;.Create(Owner: TList&lt;T&gt;);
  begin
    inherited;
    FTestList := Owner;
  end;
  
  function TOfTypeEnumeratorFactory&lt;T, TFilter&gt;.GetEnumerator: TOfTypeEnumerator&lt;T, TFilter&gt;;
  begin
    Result := TOfTypeEnumerator&lt;T,TFilter&gt;.Create(FTestList);
  end;
  
  { TTestList&lt;T&gt; }
  
  function TTestList&lt;T&gt;.OfType&lt;TFilter&gt;: IEnumerable;
  begin
    Result := TOfTypeEnumeratorFactory&lt;T,TFilter&gt;.Create(self);
  end;
  
  end.
  </code></pre>
  
  <p>Compiling this unit fails with the dreaded F2084 Internal Error:  D7837.  I can certainly do this without an enumerator, but I'd rather have one available to make the code consistent.  I had a similar compiler problem when trying to implement this on top of Spring4D, but figured I would put out a plain, vanilla Delphi issue here.</p>
  
  <p>Does anyone have an alternate implementation that actually compiles?  </p>
  
  <p>Thanks.</p>