Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put emphasis on enum entries rather than values() #3900

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions docs/topics/enum-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,42 @@ enum class IntArithmetics : BinaryOperator<Int>, IntBinaryOperator {
fun main() {
val a = 13
val b = 31
for (f in IntArithmetics.values()) {
for (f in IntArithmetics.entries) {
println("$f($a, $b) = ${f.apply(a, b)}")
}
}
```
{kotlin-runnable="true"}
{kotlin-runnable="true" kotlin-min-compiler-version="1.9"}

All enum classes implement the [Comparable](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-comparable/index.html)
interface by default. Constants in the enum class are defined in the natural order. For more information, see [Ordering](collection-ordering.md).

## Working with enum constants

Enum classes in Kotlin have synthetic methods for listing the defined enum constants and getting an enum constant by
Enum classes in Kotlin have synthetic properties and methods for listing the defined enum constants and getting an enum constant by
its name. The signatures of these methods are as follows (assuming the name of the enum class is `EnumClass`):

```kotlin
EnumClass.valueOf(value: String): EnumClass
EnumClass.values(): Array<EnumClass>
EnumClass.entries: EnumEntries<EnumClass> // specialized List<EnumClass>
```

Below is an example of these methods in action:
Below is an example of them in action:

```kotlin
enum class RGB { RED, GREEN, BLUE }

fun main() {
for (color in RGB.values()) println(color.toString()) // prints RED, GREEN, BLUE
for (color in RGB.entries) println(color.toString()) // prints RED, GREEN, BLUE
println("The first color is: ${RGB.valueOf("RED")}") // prints "The first color is: RED"
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="rgb-enums-kotlin"}
{kotlin-runnable="true" kotlin-min-compiler-version="1.9" id="rgb-enums-kotlin"}

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

In Kotlin 1.9.0, the `entries` property is introduced as a replacement for the `values()` function. The
`entries` property returns a pre-allocated immutable list of your enum constants. This is particularly useful when you
are working with [collections](collections-overview.md) and can help you avoid [performance issues](https://github.com/Kotlin/KEEP/blob/master/proposals/enum-entries.md#examples-of-performance-issues).

For example:
```kotlin
enum class RGB { RED, GREEN, BLUE }

fun main() {
for (color in RGB.entries) println(color.toString())
// prints RED, GREEN, BLUE
}
```
Prior to the introduction of `entries` in Kotlin 1.9.0, the `values()` function was used to retrieve an array of enum constants.

Every enum constant also has properties: [`name`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/name.html)
and [`ordinal`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/ordinal.html), for obtaining its name and
Expand All @@ -124,7 +112,7 @@ enum class RGB { RED, GREEN, BLUE }

fun main() {
//sampleStart
println(RGB.RED.name) // prints RED
println(RGB.RED.name) // prints RED
println(RGB.RED.ordinal) // prints 0
//sampleEnd
}
Expand Down Expand Up @@ -172,4 +160,4 @@ printAllValues<RGB>()
> The `enumEntries<T>()` function is Experimental. To use it, opt in with `@OptIn(ExperimentalStdlibApi)`, and
> [set the language version to at least 1.9](gradle-compiler-options.md#attributes-common-to-jvm-and-js).
>
{type="warning"}
{type="warning"}