如何在泛型 class 中声明枚举类型的集合类型
How to declare a set type of an enum type within a generic class
我在 Delphi XE4 中遇到了一些奇怪的行为。
我无法在泛型 class 中声明 set
类型,其中序数类型在相同的 class.
中声明
例如:
TTest<T> = class(TObject)
type
TEnumType = (eOne, eTwo, eThree);
TEnumTypes = set of TEnumType;
end;
以上不编译。编译器发出错误“E2001:需要序数类型”。
非泛型 class 喜欢
TTest = class(TObject)
type
TEnumType = (eOne, eTwo, eThree);
TEnumTypes = set of TEnumType;
end;
编译。
要使泛型 class 编译成功,必须在 class 之外声明序数类型:
TEnumType = (eOne, eTwo, eThree);
TTest<T> = class(TObject)
type
TEnumTypes = set of TEnumType;
end;
- 这种行为是否被视为错误?如果是的话,以后的版本修复了吗?
- 有人有其他解决方法吗?我想在 class 中声明类型,因为它们专门用于此 class.
的私有部分
这确实是一个bug,在以后的版本中修复了。例如,您的代码在 XE7 中编译。它很可能会在 XE5 或 XE6 中编译,但我没有立即将它们交给检查。
从问题跟踪器来看,它似乎是围绕 XE3/XE4 的回归,已在更高版本中得到修复:
我在 Delphi XE4 中遇到了一些奇怪的行为。
我无法在泛型 class 中声明 set
类型,其中序数类型在相同的 class.
例如:
TTest<T> = class(TObject)
type
TEnumType = (eOne, eTwo, eThree);
TEnumTypes = set of TEnumType;
end;
以上不编译。编译器发出错误“E2001:需要序数类型”。
非泛型 class 喜欢
TTest = class(TObject)
type
TEnumType = (eOne, eTwo, eThree);
TEnumTypes = set of TEnumType;
end;
编译。
要使泛型 class 编译成功,必须在 class 之外声明序数类型:
TEnumType = (eOne, eTwo, eThree);
TTest<T> = class(TObject)
type
TEnumTypes = set of TEnumType;
end;
- 这种行为是否被视为错误?如果是的话,以后的版本修复了吗?
- 有人有其他解决方法吗?我想在 class 中声明类型,因为它们专门用于此 class. 的私有部分
这确实是一个bug,在以后的版本中修复了。例如,您的代码在 XE7 中编译。它很可能会在 XE5 或 XE6 中编译,但我没有立即将它们交给检查。
从问题跟踪器来看,它似乎是围绕 XE3/XE4 的回归,已在更高版本中得到修复: