Giter VIP home page Giter VIP logo

how-to-code-enumerations-in-fortran's Introduction

How-to-Code-Enumerations-in-Fortran

THIS REPOSITORY SHOWS A SIMPLE WAY TO CODE ENUMERATED TYPES IN FORTRAN

Overview

In programming, enumerations (enums, enumerated types) are used to make our source code more readable and also to make the use of objects within our coding more safe. I recently found that many programmers may be unaware about the possibility to code enums in Fortran. (For an example, see this entry in wikipedia: https://en.wikipedia.org/wiki/Enumerated_type#Fortran). The Fortran standard may not specifically mention it, but compilers, and thus the Fortran language, do easily allow to emulate integer-based enumerations since Fortran 95(/90?). The situation has even improved with newer compilers, see the example and explanations below.

Fortran Enum Examples

See the example code in the src folder.

Explanations

The example's EnumColors_Values type should be PRIVATE with today's compilers (as shown in the example code), but I believe to remember that Fortran 95 compilers earlier on (back in the 1990s) did require this to be PUBLIC.

To make our (integer-based) enum type more safe to use, we choose somewhat more unique integer values for it, like that:   TYPE (EnumColors_Values), PUBLIC, PARAMETER :: EnumColors = EnumColors_Values (157839,230972,387150)

The src folder does also contain the Fortran_2003_enum_example.f90, using Fortran 2003 enum, bind(c). With that, we found it helpful to use the first enumerator to name the enumeration (Colors):
...
public :: Colors, Blue, Red, Green
enum, bind(c)
  enumerator :: Colors = 0
  enumerator :: Blue = 157839
  enumerator :: Red = 230972
  enumerator :: Green = 387150
end enum
...
integer(kind(Colors)) :: MyColor
MyColor = Green

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.