Enums in Julia
Our enum type ServerState
has an underlying Int
type in Julia. Enumerated types (enums) are special in that they provide a way to define a type with a limited set of possible values, each with a distinct name.
By implementing the Base.show
method, 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 in Julia.
The transition
function simulates a state transition for a server; it takes the existing state and returns a new state.
To run the program, put the code in a file and use Julia to execute it.
Now that we can run and build basic Julia programs, let’s learn more about the language.