enum Value Of
Требования к платформе и версии: Common (1.1), JS (1.1), JVM (1.1), Native (1.3), Wasm-JS (1.8), Wasm-WASI (1.8)
expect inline fun <T : Enum<T>> enumValueOf(name: String): T
Возвращает элемент перечисления типа T с указанным name.
name должно точно совпадать с именем существующей константы перечисления типа T (с учётом регистра).
Исключения
Примеры
import kotlin.enums.enumEntries
import kotlin.test.*
fun main() {
//sampleStart
// enum class Direction { NORTH, SOUTH, EAST, WEST }
val east = enumValueOf<Direction>("EAST")
println(east) // EAST
// The lookup fails if a string does not correspond to any of the enum entries
// enumValueOf<Direction>("UP") // will fail with IllegalArgumentException
// enumValueOf<Direction>("") // will fail with IllegalArgumentException
// The lookup is case-sensitive and fails if the name doesn't match any constant
// enumValueOf<Direction>("east") // will fail with IllegalArgumentException
//sampleEnd
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/enum-value-of.html