Explicitly scoped enums

Oxygene syntax (aka Delphi Prism) for enumerations was recently added to DWScript, with both enum and flags contextual keywords.

Enumerations scoping was already in, but optional, using these keywords make scoping explicitly required.

   TMyEnum = enum (Alpha, Beta, Gamma);
e := TMyEnum.Alpha; // works
e := Alpha; // syntax error

Since the scoping with this declaration syntax is explicit, you’re encouraged not to use prefixing for the enumeration elements, removal of the leading “T” being optional.

Classic enumerations (without keyword) stay globally scoped and unchanged for now (so this addition is fully backward-compatible).

Besides adding support for one more Oxygene syntax elements (alongside method, implies, contracts, etc.), this also provides a cleaner syntax than the {$SCOPEDENUMS} directive-based  approach introduced with Delphi XE2, with which you end up having to wrap enum declarations between directives (cf. VCL & FMX source).

One thought on “Explicitly scoped enums

  1. I like it this way more, it makes more sense, I would go even further and say
    unit|class.Enum.AEnumName makes code much more cleaner and safe, but that’s just my own opinion.
    Others would worry about verbosity, but let’s be honest, how many “begin”‘s you write/hour? :p

Comments are closed.