Enums in Racket
Our enum type ServerState
has an underlying int
type.
The possible values for ServerState
are defined as constants. Racket doesn’t have a direct equivalent of Go’s iota
, but we can enumerate them manually.
By implementing a function to convert ServerState
values to strings, 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 provide some degree of compile-time type safety.
transition
emulates a state transition for a server; it takes the existing state and returns a new state.
To run this program, open a Racket REPL or script file and execute the main
function.
The output of the program will look like this: