Enums in Miranda
On this page
Based on the input, the target language specified is “Python”. Here’s the translated code example in Markdown format suitable for Hugo.
Enumerated types (enums) are a special case of sum types. An enum is a type that has a fixed number of possible values, each with a distinct name. Go doesn’t have an enum type as a distinct language feature, but enums are simple to implement using existing language idioms.
Example
Our enum type ServerState
uses the int
type as an underlying type.
The possible values for ServerState
are defined as constants. The Enum
class in Python ensures that each value is unique and iterable.
By implementing the __str__
method, values of ServerState
can be printed out or converted to strings.
If we have a value of type int
, we cannot directly pass it to a function expecting ServerState
without converting it. This provides some degree of compile-time type safety for enums.
transition
emulates a state transition for a server; it takes the existing state and returns a new state.
To run the program, save the code to a file, for example enums.py
, and execute it using Python.
Next example: Struct Embedding.
This example shows how to use enums in Python to achieve similar functionality as described in the original example.