简体   繁体   English

如何在泛型类中声明枚举类型的集合类型

[英]How to declare a set type of an enum type within a generic class

I came across some strange behaviour in Delphi XE4. 我在Delphi XE4中遇到了一些奇怪的行为。

I can't declare a set type within a generic class, where the ordinal type is declared within the same class. 我无法在泛型类中声明set类型,而在同一类中声明了序数类型。

For example: 例如:

TTest<T> = class(TObject)
type
  TEnumType  = (eOne, eTwo, eThree);
  TEnumTypes = set of TEnumType;
end;

The above does not compile. 上面没有编译。 The compiler emits error " E2001: Ordinal type required ". 编译器发出错误“ E2001:需要序数类型 ”。

A non-generic class like 一个非通用类

TTest = class(TObject)
type
  TEnumType  = (eOne, eTwo, eThree);
  TEnumTypes = set of TEnumType;
end;

does compile. 确实可以编译。

For the generic class to compile successfully, the ordinal type has to be declared outside the class: 为了使泛型类成功编译,必须在类外声明序数类型:

TEnumType  = (eOne, eTwo, eThree);
TTest<T> = class(TObject)
type
  TEnumTypes = set of TEnumType;
end;

  1. Is this behaviour considered a bug? 这种行为是否被视为错误? If yes, has it been fixed in a later version? 如果是,它是否已在更高版本中修复?
  2. Does anyone have another workaround? 有人有其他解决方法吗? I wanted to declare the types within the class because they are used exclusively in private parts of this class. 我想在类中声明类型,因为它们专用于此类的私有部分。

This is indeed a bug that is fixed in later versions. 这确实是一个错误,已在更高版本中修复。 Your code compiles in XE7 for instance. 例如,您的代码在XE7中编译。 Quite possibly it will compile in XE5 or XE6, but I don't have them immediately to hand to check. 它很可能会在XE5或XE6中编译,但是我没有立即检查它们。

From looking at the issue tracker it seems to be a regression around XE3/XE4 which got fixed in later versions: 从问题跟踪器来看,它似乎是围绕XE3 / XE4的回归,该回归在以后的版本中得到了修复:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM