Enum Classes

The most basic usage of enum classes is implementing type-safe enums:

  1. enum class Direction {
  2. NORTH, SOUTH, WEST, EAST
  3. }

Each enum constant is an object. Enum constants are separated with commas.

Initialization

Since each enum is an instance of the enum class, they can be initialized as:

  1. enum class Color(val rgb: Int) {
  2. RED(0xFF0000),
  3. GREEN(0x00FF00),
  4. BLUE(0x0000FF)
  5. }

Anonymous Classes

Enum constants can also declare their own anonymous classes with their corresponding methods, as well as overriding base methods.

  1. enum class ProtocolState {
  2. WAITING {
  3. override fun signal() = TALKING
  4. },
  5. TALKING {
  6. override fun signal() = WAITING
  7. };
  8. abstract fun signal(): ProtocolState
  9. }

If the enum class defines any members, separate the enum constant definitions from the member definitions with a semicolon.

Enum entries cannot contain nested types other than inner classes (deprecated in Kotlin 1.2).

Implementing Interfaces in Enum Classes

An enum class may implement an interface (but not derive from a class), providing either a single interface members implementation for all of the entries, or separate ones for each entry within its anonymous class. This is done by adding the interfaces to the enum class declaration as follows:

  1. import java.util.function.BinaryOperator
  2. import java.util.function.IntBinaryOperator
  3. //sampleStart
  4. enum class IntArithmetics : BinaryOperator<Int>, IntBinaryOperator {
  5. PLUS {
  6. override fun apply(t: Int, u: Int): Int = t + u
  7. },
  8. TIMES {
  9. override fun apply(t: Int, u: Int): Int = t * u
  10. };
  11. override fun applyAsInt(t: Int, u: Int) = apply(t, u)
  12. }
  13. //sampleEnd
  14. fun main() {
  15. val a = 13
  16. val b = 31
  17. for (f in IntArithmetics.values()) {
  18. println("$f($a, $b) = ${f.apply(a, b)}")
  19. }
  20. }

Working with Enum Constants

Enum classes in Kotlin have synthetic methods allowing to list the defined enum constants and to get an enum constant by its name. The signatures of these methods are as follows (assuming the name of the enum class is EnumClass):

  1. EnumClass.valueOf(value: String): EnumClass
  2. EnumClass.values(): Array<EnumClass>

The valueOf() method throws an IllegalArgumentException if the specified name does not match any of the enum constants defined in the class.

Since Kotlin 1.1, it’s possible to access the constants in an enum class in a generic way, using the enumValues<T>() and enumValueOf<T>() functions:

  1. enum class RGB { RED, GREEN, BLUE }
  2. inline fun <reified T : Enum<T>> printAllValues() {
  3. print(enumValues<T>().joinToString { it.name })
  4. }
  5. printAllValues<RGB>() // prints RED, GREEN, BLUE

Every enum constant has properties to obtain its name and position in the enum class declaration:

  1. val name: String
  2. val ordinal: Int

The enum constants also implement the Comparable interface, with the natural order being the order in which they are defined in the enum class.