enumValueOf

expect inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.1

Throws

if no enum constant with the specified name exists in T.

Samples

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
}
actual inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.1

Throws

if no enum constant with the specified name exists in T.

Samples

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
}
actual inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.1

Throws

if no enum constant with the specified name exists in T.

Samples

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
}
actual inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.3

Throws

if no enum constant with the specified name exists in T.

Samples

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
}
actual inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.8

Throws

if no enum constant with the specified name exists in T.

Samples

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
}
actual inline fun <T : Enum<T>> enumValueOf(name: String): T(source)

Returns the enum entry of type T with the specified name.

The name must exactly match an existing enum constant of type T (case-sensitive).

Since Kotlin

1.8

Throws

if no enum constant with the specified name exists in T.

Samples

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
}