Effective Kotlin Item 41: Use enum to represent a list of values
This is a chapter from the book Effective Kotlin. You can find it on LeanPub or Amazon.
When we have to represent a constant set of possible options, a classic choice is 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 a bad practice to define mutable variables in enum classes, because they are static for each item, so this way we create a global static mutable state (see Item 1: Limit mutability). However, this functionality is often used to attach some constant values to each item. These constant values can be attached during the creation of each item using the primary constructor:
Kotlin enums can even have methods whose implementations are also item-specific. When we define them, the enum class itself needs to define an abstract method, and each item must override it:
However, 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 a when
expression, we do not need to add an else
clause when we cover all the values.
The power of enum is that its items are specific and constant. We can get all the items using this enum’s companion object’s values()
function or the top-level enumValueOf
function. We can also read enum from a String
using its companion object’s valueOf(String)
or top-level enumValueOf(String)
. All enums are a subtype of Enum<T>
.
Iterating over enum values is easy. All enums have the ordinal
property and implement the Comparable
interface. Enums also automatically implement toString
, hashCode
and 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 for representing a concrete set of constant values. But how do they compare to sealed classes?
Enum or a sealed class?
As shown in Item 39: Use sealed classes and interfaces to express restricted hierarchies; sealed classes and interfaces can also represent a set of values, but all their subclasses have to be object declarations.
For just a set of values, enum should be preferred: sealed classes cannot be automatically serialized or deserialized; they are not so easy to iterate over (although we can do it with reflection); and they do not have a natural order. However, there are some cases when we might choose sealed classes with object subclasses anyway: classes can keep values, so if we think we might need to transform objects into classes at some point, we should prefer sealed classes. For instance, if we define messages to some actor, we should use sealed classes even if all the messages are now object declarations. This is because it is likely we will need to transform some of these objects into classes in the future.
When messages need to hold data, it is a clear-cut case: we need classes, not merely an enum. This is how our payment types, which hold the data required to make transactions, could be represented:
Summary
- The advantage of enum classes is that they can be serialized and deserialized out of the box. They have the companion object methods
values()
andvalueOf(String)
. We can also get enum values by the type using theenumValues()
andenumValueOf(String)
functions. Each enum value hasordinal
, and we can hold per-item data. They are perfect for representing a constant set of possible values. - Enum classes represent a concrete set of values, while sealed classes represent a concrete set of classes. Since these classes can be object declarations, we can use sealed classes to a certain degree instead of enums, but not the other way around.