Clicky

Fortran Wiki
Typed enumerators

Proposal: Support a named type for enumerators, and perhaps other intrinsic types as well.

NOTE: This has already been discussed in Fortran standards, and was implemented in at least on version of Cray Fortran

Fortran2003 defines the ENUMERATOR type, similar to C enum. Unlike C, a specific enumerator cannot be a defined type. A named type attribute helps avoid errors by ensuring that a given enumerator is used in the correct context, and can be used to distinguish specific procedures in a generic interface.

A [somewhat ugly] work-around is to encapsulate an integer enum inside of a derived type. It is not a clean work-around because a derived type cannot be passed by value.

A possible syntax is to follow the derived-type syntax:

ENUM, BIND(C) :: COLOR
  ENUMERATOR :: RED = 4, BLUE = 9
  ENUMERATOR YELLOW
END ENUM

TYPE(COLOR) :: MYCOLOR = BLUE

Unlike a true derived type, this type is scalar and has no member-selector syntax.

A different syntax is required to support other intrinsic types. It is not unusual for an integer to be used as a unique object ID, so confering a type name onto a scalar integer is reasonable.

Maybe define an ALIAS attribute? Maybe there was also a proposal for this sort of alias that never made the standards? In this case, the TYPE stantement is a single line rather than a multi-line construct. For example:

TYPE, ALIAS(INTEGER(8)) :: OBJECT_ID