Is the specification for the new enumerator type
fixed for F202X? I’ve just been reading the proposed spec and while it’s missing a few features that would be incredibly useful (user-defined values, IO, character conversion), it also has a massive flaw: name scope. The missing features could always be added in later revisions, but the current proposal for the scope of names (“class one names”) would hamstring it on arrival.
It’s very possible that I have misunderstood the status of the current design, in which case I would be very happy to be corrected.
From John Reid’s great explainer on the new features of F202X:
enumeration type :: colour enumerator :: red, orange, green end type type(colour) light : if (light==red) ...
…
The names of the types and their constants have exactly the same scoping rules as for other types and constants; for example, a procedure that contains the above declaration must not contain a real variable named red.
With this current rule for the scope of the names, it means it would be impossible to have two enumerators with the same name in the same scope:
module colours
enumeration type :: additive_primary_colours
enumerator :: red, green, blue
end type
enumeration type :: subtractive_primary_colours
enumerator :: red, yellow, blue
end type
end module colours
Here the red
and blue
enumerators would clash and so this wouldn’t compile. Basically every other language since C has had some sort of namespacing for enums. C++11 introduced enum class
for exactly this purpose; C#, Java, Swift, etc. have all had namespaced/scoped enums from the beginning. They are a Good Idea.
From what I can tell by reading through the proposals on the J3 site, the initial proposals did include namespacing the enumerators, but this seems to have been removed in J3/21-120r3.
The relevant comments on that document are:
(13) I note that enumerators as class one names is not only simpler
but what WG5 asked us to do in the first place.
(13) Fortran already has basic namespace management (USE ONLY and
renaming). Some ideas for extensions to that have already been
floated (for a future revision, possibly F202y); we should not
preempt that with a complicated feature here.
For the first comment, simpler is not always better. C’s enum
is simpler than C++'s enum class
, but enum class
solves lots of problems with enum
.
And I think that the second comment only applies to importing enumerators into another scope. As it stands, it would not be possible to declare two enums with clashing names in the same module, and I think that is a significant flaw in the current design and one that would be impossible to fix later.
Does anyone know if the current design is set in stone, or if it’s still possible to alter it?