Generic method returning generic interface in Delphi 2010

前端 未结 2 1357
南旧
南旧 2021-01-02 17:58

Given the code below, wich is a very trimmed down version of the actual code, I get the following error:

[DCC Error] Unit3.pas(31): E2010 Incompatible types: \'IXL

相关标签:
2条回答
  • 2021-01-02 18:36

    With three minor modification (IInterface, FindAll with "S: class" [Thanks Mason] and the typecasts in FindAll) I got it compiling.

    Full code:

    unit Unit16;
    
    interface
    
    uses
      Generics.Collections;
    
    type
      IXList<T> = interface
      end;
    
      TXList<T: class> = class(TList<T>, IInterface, IXList<T>)
      protected
        FRefCount: Integer;
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
      public
        function Find: IXList<T>;
        function FindAll<S: class>: IXList<S>;
      end;
    
    implementation
    uses Windows;
    
    function TXList<T>.Find: IXList<T>;
    begin
      Result := TXList<T>.Create;
    end;
    
    function TXList<T>.FindAll<S>: IXList<S>;
    begin
      Result := IXList<S>(IUnknown(TXList<S>.Create));
    end;
    
    function TXList<T>.QueryInterface(const IID: TGUID; out Obj): HResult;
    begin
      Result := E_NoInterface;
    end;
    
    function TXList<T>._AddRef: Integer;
    begin
      InterlockedIncrement(FRefCount);
    end;
    
    function TXList<T>._Release: Integer;
    begin
      InterlockedDecrement(FRefCount);
      if FRefCount = 0 then Self.Destroy;
    end;
    
    end.
    
    0 讨论(0)
  • 2021-01-02 18:48

    That definitely looks like a compiler error. They're saying how they've focused a lot of effort into improving Generics issues for the next version, Delphi XE. When it gets released, which should be within the next couple weeks, download the preview and see if that will compile now. If not, try filing a bug report with QC.

    Also, FindAll<S> should probably be declared as function FindAll<S: class>: IXList<S>;. That doesn't fix the error, but a working compiler would probably give you an error on that.

    0 讨论(0)
提交回复
热议问题