Using nested type declared in a generic class within the generic interface that the class is implementing

StackOverflow https://stackoverflow.com/questions/18677084

Domanda

I have a generic List-like class tMyList<B> with a method Each() which iterated over each element and calls anonymous procedure tMyList<A>.enumProc with paramenter - the current item of type <B>. I want to implement the class as an Interface for easier lifetime management.

The problem is I cannot declare the Each method in the iMyList<A> interface, because tMyList<A>.enumProc type is unknown. As far as I know Interfaces does not support nested types?

Here is the code:

  tMyList<B> = class;

  iMyList<A> = interface
    procedure each(enumProcedure: iMyList<A>.enumProc); // ERROR - Undeclared identifier: 'enumProc'
  end;

  tMyList<B> = class(tInterfacedObject, iMyList<B>)
    type
      enumProc = reference to procedure(item: iMyList<B>);
    public
      procedure each(enumProcedure: enumProc);
  end;

* Implementing Enumerator is not an option in this particular case

È stato utile?

Soluzione

The only way that you can make this work is to define the procedural type outside of the implementing class. Like this:

type
  IMyIntf<A> = interface;

  TMyProc<A> = reference to procedure(Intf: IMyIntf<A>);

  IMyIntf<A> = interface
    procedure Foo(Proc: TMyProc<A>);
  end;

  TMyClass<A> = class(TInterfacedObject, IMyIntf<A>)
    procedure Foo(Proc: TMyProc<A>);
  end;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top