Enum values in their own namespaces/scopes: Scoped Enums (Delphi)
Posted by jpluimers on 2018/08/29
A while ago, I needed several enum types in the same unit with overlapping enumeration values.
Putting each in an encompassing type wasn’t possible and I didn’t want to put each in their own unit.
Luckily, Delphi 2009 introduced the “scoped enum” feature effectively promoting the enumeration type into a scope or namespace.
It is only available at the source code level, as – at least up until Delphi 10.1 Berlin – it is not part of the compiler settings in the project options (see screenshot below).
Since the below was hard to find combined with the word “namespace” I’ve quoted it in full (note an earlier version of the post had a typo here as it was copied from the Delphi 2009 documentation which had SCOPEDEUNMS
wrong):
Type Switch Syntax {$SCOPEDENUMS ON}, or {$SCOPEDENUMS OFF} Default {$SCOPEDENUMS OFF} Scope LocalRemarks
The $SCOPEDENUMS directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS affects only definitions of new enumerations, and only controls the addition of the enumeration’s value symbols to the global scope.
In the {$SCOPEDENUMS ON} state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum. For example:
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;Note that this is also valid:
Writeln(Integer(TFoo.A));Even though TFoo was not declared with $SCOPEDENUMS ON, the A value can still be explicitly resolved using the enumeration name.
Edit 20180920
Ooops. Thanks for notifying me!
Sorry for this: it is my word-blindness that kicked in, which makes it really hard for me to spot typos.
I just did a check:
- it is wrong in the Delphi 2009 and 2010 documentation
- it is right in the Delphi XE and up documentation
- it is wrong in the French and German Appmethod 1.17 documentation (but correct in the English)
Links:
- [WayBack] Scoped Enums (Delphi): Delphi 2009
- [WayBack] Scoped Enums (Delphi) – RAD Studio 2010
- [WayBack] Scoped Enums (Object Pascal) – Appmethod 1.17 Topics
- [WayBack] Bereichsabhängige Aufzählungen (Object Pascal) – Appmethod 1.17 Topics
- [WayBack] Enumérations de portée (Object Pascal) — Appmethod 1.17 Topics
- [WayBack] Scoped Enums (Delphi) – RAD Studio XE
–jeroen
Source: [WayBack] Scoped Enums (Delphi)
via [WayBack] own namespace for nested enums in Delphi – Stack Overflow
The Project Options do not have a “scoped enum” entry under the compiler settings:
Jacek said
F…k… I copied from your site {$ SCOPEDEUNMS ON} and I’ve pasted in very many places in the code … then I got a compilation errors, I’m looking and I do not see … after a while it dawned on me … you made a typo in …EUNMS:-)
I’m correcting now my code ;-)
jpluimers said
Ooops. Thanks for notifying me!
Sorry for this: it is my word-blindness that kicked in, which makes it really hard for me to spot typos.
I just did a check, for which I adapted the post.
Links:
Garry Duren said
Very good to know, thanks! No setting in Project Options? How to set this for an entire project?
jpluimers said
No, it is not in the project options, which means you cannot set it for the full project unless you start using include files.
I have added a screenshot of the project options to the article. Thanks for mentioning that.