Enums in Fortress
To implement enumerated types (enums) in Python we will use enums provided by the enum
module in Python. The given explanation and code example translated to Python will look like this:
Our enum type ServerState
has an underlying int
type.
The possible values for ServerState
are defined as constants using the auto() function to generate successive constant values automatically.
By implementing the __str__
method, values of ServerState
can be printed out or converted to strings. In case there are many possible values, the enum can still be leveraged effectively.
The transition
function emulates a state transition for a server; it takes the existing state and returns a new state. Here’s how you can run the program.
This provides some degree of compile-time type safety for enums since you cannot pass an arbitrary int
to the transition
function.