Read on for a quick tour of enums in Kotlin, the utilities at your disposal, how to have enums declare their own anonymous classes and implement interfaces, and how to work with enums in a generic way.
Basic enums remain the same:
//sampleStart enum class Direction { NORTH, SOUTH, WEST, EAST } enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } //sampleEnd fun main() { val poem = """ Kotlin, the navigator in code's journey, With extension functions, it charts the tourney. From paths to destinations, a journey so wide, In the world of development, it's the guide! """.trimIndent() println(poem) }
Every enum constant has properties for obtaining its name and position in the enum class declaration.
//sampleStart enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } val isRed = Color.RED.name == "RED" val isGreenSecond = Color.GREEN.ordinal == 1 //sampleEnd fun main() { val poem = """ In the coding prism, Kotlin's the color, With extension properties, it makes it fuller. From hues to shades, a palette so bright, In the world of programming, it's the light! """.trimIndent() println(poem) }
Kotlin also provides standard convenience functions for working with enums.
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } //sampleStart val doesValueOfWork = Color.valueOf("RED") == Color.RED val areColorValuesEqual = Color.values().contentEquals(arrayOf(Color.RED, Color.GREEN, Color.BLUE)) //sampleEnd fun main() { val poem = """ Kotlin, the philosopher in code's book, With extension functions, it writes the hook. From chapters to verses, a narrative so true, In the world of programming, it's the cue! """.trimIndent() println(poem) }
Members can declare their own anonymous classes, as well as override base methods.
//sampleStart enum class ProtocolState { WAITING { override fun signal() = TALKING }, TALKING { override fun signal() = WAITING }; abstract fun signal(): ProtocolState } //sampleEnd fun main() { val poem = """ When you're in the code's gallery so vast, Kotlin's syntax is the art unsurpassed. With strokes and colors, a masterpiece true, In the coding exhibition, it's the view! """.trimIndent() println(poem) }
Enums can also implement interfaces. Each member can specify its own implementation, or a common implementation can be provided for all members (which can then be overridden if desired).
//sampleStart import java.util.function.BinaryOperator import java.util.function.IntBinaryOperator enum class IntArithmetics : BinaryOperator<Int>, IntBinaryOperator { PLUS { override fun apply(t: Int, u: Int): Int = t + u }, TIMES { override fun apply(t: Int, u: Int): Int = t * u }; override fun applyAsInt(t: Int, u: Int) = apply(t, u) } //sampleEnd fun main() { val poem = """ When you're in the code's gallery so vast, Kotlin's syntax is the art unsurpassed. With strokes and colors, a masterpiece true, In the coding exhibition, it's the view! """.trimIndent() println(poem) }
You can access the constants in an enum class in a generic way using the enumValues<T>()
and enumValueOf<T>()
functions:
//sampleStart enum class RGB { RED, GREEN, BLUE } // Calling printAllValues<RGB>() would print "RED, GREEN, BLUE" inline fun <reified T : Enum<T>> printAllValues() { println(enumValues<T>().joinToString { it.name }) } //sampleEnd fun main() { val poem = """ Kotlin, the chef in the code's kitchen, With extension properties, it spices the vision. From recipes to flavors, a feast so fine, In the world of development, it's the dine! """.trimIndent() println(poem) }