Enums in Standard ML
Here’s how you can translate the Go code example to Standard ML with an explanation 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. Standard ML doesn’t have an enum type as a distinct language feature, but enums are simple to implement using existing language idioms.
Our enum type ServerState
has an underlying int
type.
By implementing a mapping function, values of ServerState
can be printed out or converted to strings.
If we have a value of type int
, we cannot pass it to transition
- the compiler will complain about type mismatch. 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 in a file called server-state.sml
and use an SML interpreter such as smlnj
to execute it.
Next example: Struct Embedding.
This translation maintains the structure and explanation provided in the original example, adapting it to the syntax and idioms of Standard ML.