How to set a forward declaration with generic types under Delphi 2010?

I run into what seems to be a very classical problem: An item and a collection class, both referencing each other, that require a forward declaration. I'm using Delphi 2010 with update 5.

This works well with non generic classes but I can't workaround the E2086 error with generic types:

type
  // Forward declarations
  TMyElement = class; // E2086: Type 'TMyElement' is not yet completely defined

  TMyCollection<T:TMyElement> = class
    //
  end;

  TMyElement = class
    FParent: TMyCollection<TMyElement>;
  end;

The same issue happens when switching the class declaration order.

I didn't found any reference to this issue here or in QualityCentral (other issues with E2086 were found, but not related to this use case)

The only workaround I have for now is to declare the parent as TObject, and cast it to the collection generic type when required (not a clean solution...)

How did you workaround this issue, or forward-declare your generic classes?

Thanks,

[Edit Oct 22, 2011] Follow up on QualityCentral: I reported this bug in quality central here

This has been closed recently by EMB with the following resolution status: Resolution: As designed Resolved in build: 16.0.4152

I only have Delphi 2010. Could someone confirm that it has been fixed in Delphe XE2 Update1, or does it mean that it works 'as expected'?

[Edit Oct 23, 2011] Final answer from EMB: EMB confirmed today that using forward declaration of a generic type is not supported by the actual Delphi compiler. You can see their answer in QC, with the link provided above.


You can work around it by declaring an ancestor class:

type
  TBaseElement = class
  end;

  TMyCollection<T: TBaseElement> = class
  end;

  TMyElement = class(TBaseElement)
  private
    FParent: TMyCollection<TBaseElement>;
  end;