title: Generic method returning generic interface in Delphi 2010
body: <p>Given the code below, wich is a very trimmed down version of the actual code, I get the following error:  </p>
  
  <p>[DCC Error] Unit3.pas(31): E2010 Incompatible types: 'IXList&lt;Unit3.TXList&lt;T&gt;.FindAll.S&gt;' and 'TXList&lt;Unit3.TXList&lt;T&gt;.FindAll.S&gt;'  </p>
  
  <p>In the FindAll&lt;S&gt; function.</p>
  
  <p>I can't really see why since there is no problem with the previous very similar function.</p>
  
  <p>Can anyone shed some light on it?<br>
  Is it me or is it a bug in the compiler?</p>
  
  <p>unit Unit3;</p>
  
  <pre><code>interface
  uses Generics.Collections;
  
  type
    IXList&lt;T&gt; = interface
    end;
  
    TXList&lt;T: class&gt; = class(TList&lt;T&gt;, IXList&lt;T&gt;)
    protected
      FRefCount: Integer;
      function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
      function _AddRef: Integer; stdcall;
      function _Release: Integer; stdcall;
    public
      function Find: IXList&lt;T&gt;;
      function FindAll&lt;S&gt;: IXList&lt;S&gt;;
    end;
  
  implementation
  uses Windows;
  
  function TXList&lt;T&gt;.Find: IXList&lt;T&gt;;
  begin
    Result := TXList&lt;T&gt;.Create;
  end;
  
  function TXList&lt;T&gt;.FindAll&lt;S&gt;: IXList&lt;S&gt;;
  begin
    Result := TXList&lt;S&gt;.Create; // Error here  
  end;
  
  function TXList&lt;T&gt;.QueryInterface(const IID: TGUID; out Obj): HResult;
  begin
    Result := E_NoInterface;
  end;
  
  function TXList&lt;T&gt;._AddRef: Integer;
  begin
    InterlockedIncrement(FRefCount);
  end;
  
  function TXList&lt;T&gt;._Release: Integer;
  begin
    InterlockedDecrement(FRefCount);
    if FRefCount = 0 then Self.Destroy;
  end;
  
  end.
  </code></pre>
  
  <p>Thanks for the answers!
  It seems like a compiler bug with an acceptable workaround available.</p>
  
  <p>With the interface declared as</p>
  
  <pre><code>IXList&lt;T: class&gt; = interface
     function GetEnumerator: TList&lt;T&gt;.TEnumerator;
  end;
  </code></pre>
  
  <p>and findall implemented as</p>
  
  <pre><code>function TXList&lt;T&gt;.FindAll&lt;S&gt;: IXList&lt;S&gt;;
  var
    lst: TXList&lt;S&gt;;
    i: T;
  begin
    lst := TXList&lt;S&gt;.Create;
    for i in Self do
      if i.InheritsFrom(S) then lst.Add(S(TObject(i)));
  
    Result := IXList&lt;S&gt;(IUnknown(lst));
  end;
  </code></pre>
  
  <p>I got it working in a simple example.</p>
  
  <p>Doing something like:  </p>
  
  <pre><code>var
    l: TXList&lt;TAClass&gt;;
    i: TASubclassOfTAClass;
  begin
  .
  .
  .
  for i in l.FindAll&lt;TASubclassOfTAClass&gt; do
  begin
     // Do something with i
  end;
  </code></pre>