在Delphi 2010中如何设置通用类型的前向声明? [英] How to set a forward declaration with generic types under Delphi 2010?

查看:335
本文介绍了在Delphi 2010中如何设置通用类型的前向声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个似乎是一个非常古典的问题:一个项目和一个集合类,两个引用对象,需要一个前向声明。我使用Delphi 2010与更新5。



这对非通用类很好,但是我无法解决E2086错误与泛型类型:

 键入
//转发声明
TMyElement = class; // E2086:类型'TMyElement'尚未完全定义

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

TMyElement = class
FParent:TMyCollection< TMyElement> ;;
结束

切换类声明顺序时也会出现相同的问题。



我没有在这里或QualityCentral中找到任何这个问题的引用(E2086的其他问题被发现,但与此用例无关)



现在我唯一的解决方法是将父节点声明为TObject,并在必要时将其转换为集合泛型类型(不是一个干净的解决方案...)



你是否解决了这个问题,还是转发了你的通用类?



谢谢,



编辑2011年10月22日]跟随QualityCentral:
我在质量中心报告了这个错误 here



最近由EMB已经关闭了以下解决方案状态:
分辨率:设计
解决在构建:16.0.4152



我只有Delphi 2010.有人可以确认它h在Delphe XE2 Update1中已经修复了,还是意味着它按照预期的方式工作?



2011年10月23日编辑EMB的最终答案:
EMB今天证实,通过实际的Delphi编译器,使用通用类型的forward声明是不支持的。您可以通过上面提供的链接在QC中看到他们的答案。

解决方案

您可以通过声明祖先类来解决问题:

 键入
TBaseElement = class
end;

TMyCollection< T:TBaseElement> = class
end;

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


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;

这篇关于在Delphi 2010中如何设置通用类型的前向声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆