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

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

问题描述

我遇到了一个似乎非常经典的问题:一个项目和一个集合类,两者都相互引用,需要一个前向声明.我使用的是带有更新 5 的 Delphi 2010.

这适用于非泛型类,但我无法解决泛型类型的 E2086 错误:

类型//前向声明TMyElement = 类;//E2086: 类型 'TMyElement' 尚未完全定义TMyCollection= 班级//结尾;TMyElement = 类FParent: TMyCollection;结尾;

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

我没有在此处或 QualityCentral 中找到对此问题的任何参考(发现了 E2086 的其他问题,但与此用例无关)

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

你是如何解决这个问题的,或者如何提前声明你的泛型类?

谢谢,

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

最近 EMB 已将其关闭,解决状态如下:分辨率:按设计在构建中解决:16.0.4152

我只有 Delphi 2010.有人可以确认它已在 Delphi XE2 Update1 中修复,或者这是否意味着它按预期"工作?

[Edit Oct 23, 2011] EMB 的最终回答:EMB 今天证实,实际的 Delphi 编译器不支持使用泛型类型的前向声明.您可以通过上面提供的链接在 QC 中看到他们的答案.

解决方案

你可以通过声明一个祖先类来解决这个问题:

类型TBaseElement = 类结尾;TMyCollection= 班级结尾;TMyElement = 类(TBaseElement)私人的FParent: TMyCollection;结尾;

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天全站免登陆