Effective Kotlin Item 41: Use enum to represent a list of values
When we have to represent a constant set of possible options, a classic choice was to use an enum. For instance, if our website offers a concrete set of payment methods, we can represent them in our service using the following enum class:
Since each enum is an instance of the enum class, it can hold values that are always item-specific:
It is not a good practice to make such values mutable, as they are static per item, so we create global static mutable state (see Item 1: Limit mutability). However, this functionality is often used to attach some constant values per item. Those constant values can be attached during each item creation, using the primary constructor:
Kotlin enums can even have methods. Their implementations are also item-specific. When we define them, enum class itself needs to define an abstract method, and each item must override it:
Although this option is rarely used, because it is more convenient to use a primary constructor parameter of functional type:
An even more convenient option is to define an extension function. Notice that when we use enum values in the
when expression, we do not need to add
else clause when we cover all the values.
The power of enum is that those items are specific and constant. We can get all the items using companion object
values() function, or top-level
enumValueOf function. We can also read enum from a
String using companion object
valueOf(String), or top-level
Iterating over enum values is easy. They have the
ordinal property, implement
Comparable interface, and automatically implement
equals. Their serialization and deserialization are simple (they are represented just by name), efficient and automatically supported by most libraries for serialization (like Moshi, Gson, Jackson, Kotlinx Serialization, etc). As a result, enums are perfect to represent a concrete set of constant values. So now, how do they compare to the sealed classes?
Enum or sealed class?
Sealed classes and interfaces, as shown in Item 39: Use sealed classes and interfaces to express restricted hierarchies, can also represent a set of values, but all their subclasses would have to be object declarations.
For just a set of values, enum should be preferred. Those classes will not be automatically serialized or deserialized, they are not so easy to iterate over them (although we can do it with reflection), and do not have a natural order. Although there are some cases when we might choose this anyway.
Classes can keep values, so if we consider we might at some point need that, we prefer sealed classes. For instance, if we define messages to some actor, we use sealed classes even if now all the messages are object declarations, because it is likely we will need to add one or more that are holding instance-specific data in the future.
When messages need to hold the data, it is a clear case - we need classes, not merely an enum. This is how our payment types holding data required to make transactions, could be represented:
- The advantage of enum classes is that they can be serialized and deserialized out of the box. They have companion object methods
valueOf(String). We can also get enum values by the type using
enumValueOf(String)functions. Each enum value has
ordinaland we can hold per-item data. They are perfect to represent a constant set of possible values.
- Enum classes represent a concrete set of values, while sealed classes represent a concrete set of classes. Since those classes can be object declarations, we can use sealed classes to a certain degree instead of enums, but not the other way around.