Enumeration defines a set of variables related together. It also exists in other languages. Nonetheless, It does not behave in the same way as, for example, in C. That would only allow the enum types when declared.

Note It is allowed since the Standard Fortran 2003.

Check below how to declare them:

PUBLIC :: E_VEG_ELEM_C, E_VEG_ELEM_N, E_VEG_ELEM_P
...
ENUM, BIND(C)
  ENUMERATOR :: E_VEG_ELEM_C
  ENUMERATOR :: E_VEG_ELEM_N
  ENUMERATOR :: E_VEG_ELEM_P
END ENUM

How to use them with the same scope:

USE SOME_MODULE, ONLY: E_VEG_ELEM_C
...
INTEGER :: t_veg_element
...
IF (t_veg_element == E_VEG_ELEM_C) THEN
  Do something here
  ...

In a method:

subroutine calc_elem(veg_elem)
   integer, intent(in) :: veg_elem
   ! do some magic here
end subroutine

However, it is not possible to enforce the enum type into a method. The example below, proves it:

PROGRAM main

  implicit none

  enum, bind(C)
    enumerator :: enum_veg_elems
    enumerator :: e_veg_elem_c
    enumerator :: e_veg_elem_n
    enumerator :: e_veg_elem_p
  end enum

  integer(kind(enum_veg_elems)) :: veg

  veg = e_veg_elem_c
  call calc_elem(e_veg_elem_p)
  ! it cannot be enforced (It compiles).
  call calc_elem(120)

CONTAINS

  subroutine calc_elem(veg_elem)
    integer(kind(enum_veg_elems)), intent(in) :: veg_elem
    ! do some magic here
  end subroutine

END PROGRAM main

More info: IBM, Intel

Leave a Reply

Your email address will not be published. Required fields are marked *