Enums in Squirrel
Our enum type `ServerState` has an underlying `int` type.
```kotlin
enum class ServerState(val value: Int) {
StateIdle(0),
StateConnected(1),
StateError(2),
StateRetrying(3);
}
The possible values for ServerState
are defined as constants. The enum values are automatically generated and assigned the values 0, 1, 2, and so on.
By implementing the toString()
method, values of ServerState
can be printed out or converted to strings. This can get cumbersome if there are many possible values. In such cases, reflection or auxiliary tools can be used to automate the process.
val stateName = mapOf(
ServerState.StateIdle to "idle",
ServerState.StateConnected to "connected",
ServerState.StateError to "error",
ServerState.StateRetrying to "retrying"
)
fun ServerState.toString(): String {
return stateName[this] ?: "unknown"
}
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.
fun main() {
val ns = transition(ServerState.StateIdle)
println(ns)
val ns2 = transition(ns)
println(ns2)
}
transition
emulates a state transition for a server; it takes the existing state and returns a new state.
fun transition(s: ServerState): ServerState {
return when (s) {
ServerState.StateIdle -> ServerState.StateConnected
ServerState.StateConnected, ServerState.StateRetrying -> {
// Suppose we check some predicates here to determine the next state...
ServerState.StateIdle
}
ServerState.StateError -> ServerState.StateError
else -> throw IllegalStateException("Unknown state: $s")
}
}
To run the program, compile the code using kotlinc
and then use kotlin
to execute it.
$ kotlinc enums.kt -d Enums.jar
$ kotlin -cp Enums.jar MainKt
connected
idle