
This is a chapter from the book Kotlin Essentials. You can find it on LeanPub or Amazon. It is also available as a course.
BANK_TRANSFER). Enum class elements can be referenced by the enum name, followed by a dot, and then the value name (e.g., PaymentOption.CASH). All values are typed as the enum class type.enum class PaymentOption { CASH, CARD, TRANSFER, } fun printOption(option: PaymentOption) { println(option) } fun main() { val option: PaymentOption = PaymentOption.CARD println(option) // CARD printOption(option) // CARD }
entriesproperty, which keeps a list of all the values of this enum class. It is a modern replacement of thevaluesfunction, that returns an array of elements[^13_3].valueOffunction, which parses a string into a value matching its name (this is case-sensitive) or throws an exception.
enum class PaymentOption { CASH, CARD, TRANSFER, } fun main() { val option: PaymentOption = PaymentOption.valueOf("TRANSFER") println(option) println("All options: ") val paymentOptions: List<PaymentOption> = PaymentOption.entries for (paymentOption in paymentOptions) { println(paymentOption) } } // TRANSFER // All options: // CASH // CARD // TRANSFER
enumValues and enumValueOf functions.enum class PaymentOption { CASH, CARD, TRANSFER, } fun main() { val option = enumValueOf<PaymentOption>("TRANSFER") println(option) println("All options: ") val paymentOptions = enumValues<PaymentOption>() for (paymentOption in paymentOptions) { println(paymentOption) } } // TRANSFER // All options: // CASH // CARD // TRANSFER
name- the name of this value,ordinal- the position of this value (starting from 0).
enum class PaymentOption { CASH, CARD, TRANSFER, } fun main() { val option = PaymentOption.TRANSFER println(option.name) // TRANSFER println(option.ordinal) // 2 }
Enum. This superclass guarantees the name and ordinal properties. Enum classes have properties that implement toString, equals, and hashCode, but, unlike data classes, they also have compareTo (their natural order is the order of the elements in the body).fun transactionFee(paymentOption: PaymentOption): Double = when (paymentOption) { PaymentOption.CASH -> 0.0 PaymentOption.CARD, PaymentOption.TRANSFER -> 0.05 }
import java.math.BigDecimal enum class PaymentOption(val commission: BigDecimal) { CASH(BigDecimal.ONE), CARD(BigDecimal.TEN), TRANSFER(BigDecimal.ZERO) } fun main() { println(PaymentOption.CARD.commission) // 10 println(PaymentOption.TRANSFER.commission) // 0 val paymentOption: PaymentOption = PaymentOption.entries.random() println(paymentOption.commission) // 0, 1 or 10 }
enum class PaymentOption { CASH { override fun startPayment( transaction: Transaction ) { showCashPaymentInfo(transaction) } }, CARD { override fun startPayment( transaction: Transaction ) { moveToCardPaymentPage(transaction) } }, TRANSFER { override fun startPayment( transaction: Transaction ) { showMoneyTransferInfo() setupPaymentWatcher(transaction) } }; abstract fun startPayment(transaction: Transaction) }
name and ordinal (position). We can get an array of all values using the values companion object function or the enumValues top-level function. We can also parse an enum value from String using the valueOf companion object function or the enumValueOf top-level function.[^13_2]: Extension functions are described later in this book.
[^13_3]:
entries property was introduced in Kotlin 1.9, so in projects using older versions of Kotlin you need to use values instead.