Enums in Groovy
Our enum type ServerState
has an underlying int
type.
The possible values for ServerState
are defined as constants. The special keyword iota
generates successive constant values automatically; in this case 0, 1, 2, and so on.
By implementing a mapping of ServerState
to its name, values of ServerState
can be printed out or converted to strings.
This can get cumbersome if there are many possible values. In such cases, tools can be used to automate the process.
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.
To run the program, save the code in a file named EnumsExample.groovy
and use the Groovy command-line tool to execute it:
Now that we can run and build basic Groovy programs, let’s learn more about the language.