自己的Delphi中嵌套枚举的命名空间

Chr*_*ris 9 delphi enums delphi-xe2

有没有办法将Delphi中的嵌套枚举放入自己的命名空间?

此代码生成重新声明的E2004:标识符,因为两个枚举都包含"未知".

TMyType1 = class
public type
  TMyType1Enum = (unknown, val1, val2);
public
  constructor Create();
  ...
end;

TMyType2 = class
public type
  TMyType2Enum = (unknown, other1, other2, other3); // causes E2004
public
  constructor Create();
  ...
end;
Run Code Online (Sandbox Code Playgroud)

在C++中,枚举元素的标识符都在不同的范围内(TMyType1 :: unknown和TMyType2 :: unknown).

除了标识符的前缀或后缀(MyType1EnumUnknown,MyType1EnumVal1,...,MyType2Enumunknown,...)之外,是否有可能在Delphi中实现类似的功能?

Uli*_*rdt 11

试试$SCOPEDENUMS.来自http://docwiki.embarcadero.com/RADStudio/en/Scoped_Enums_(Delphi):

type
  TFoo = (A, B, Foo);
  {$SCOPEDENUMS ON}
  TBar = (A, B, Bar);
  {$SCOPEDENUMS OFF}

begin
  WriteLn(Integer(Foo)); 
  WriteLn(Integer(A)); // TFoo.A
  WriteLn(Integer(TBar.B));
  WriteLn(Integer(TBar.Bar));
  WriteLn(Integer(Bar)); // Error
end;
Run Code Online (Sandbox Code Playgroud)