diff --git a/libraries/stdlib/src/kotlin/enums/EnumEntries.kt b/libraries/stdlib/src/kotlin/enums/EnumEntries.kt index 013b3e95921..da1f4600dac 100644 --- a/libraries/stdlib/src/kotlin/enums/EnumEntries.kt +++ b/libraries/stdlib/src/kotlin/enums/EnumEntries.kt @@ -13,7 +13,7 @@ package kotlin.enums * [EnumEntries] contains all enum entries in the order they are declared in the source code, * consistently with the corresponding [Enum.ordinal] values. * - * An instance of this interface can only be obtained from `EnumClass.entries` property. + * An instance of this interface can be obtained from `EnumClass.entries` property or with [enumEntries] function. * * #### Implementation note * All basic operations, such as `contains` and `indexOf`, are executed in constant time and are likely to be @@ -23,6 +23,21 @@ package kotlin.enums @WasExperimental(ExperimentalStdlibApi::class) public sealed interface EnumEntries> : List +/** + * Returns [EnumEntries] list containing all enum entries for the given enum type [T]. + */ +@PublishedApi +@ExperimentalStdlibApi +@SinceKotlin("1.9") +internal inline fun > enumEntries(): EnumEntries { + /* + * Implementation note: this body will be replaced with `throw NotImplementedException()` the moment + * all backends starts intrinsifying this call. + * Then this method will also be promoted to public. + */ + return enumEntries(enumValues()) +} + @PublishedApi @SinceKotlin("1.8") // Used by pre-1.9.0 JVM compiler for the feature in preview mode. Can be safely removed around 2.1 internal fun > enumEntries(entriesProvider: () -> Array): EnumEntries = EnumEntriesList(entriesProvider()) diff --git a/libraries/stdlib/test/enums/EnumEntriesFactoryTest.kt b/libraries/stdlib/test/enums/EnumEntriesFactoryTest.kt new file mode 100644 index 00000000000..b7756b00f56 --- /dev/null +++ b/libraries/stdlib/test/enums/EnumEntriesFactoryTest.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +package test.enums + +import kotlin.enums.enumEntries +import kotlin.test.* +import test.collections.behaviors.listBehavior +import test.collections.compare +import kotlin.enums.EnumEntries + +class EnumEntriesFactoryTest { + + enum class EmptyEnum + + enum class NonEmptyEnum { + A, B, C + } + + @Test + fun testEquality() { + assertEquals(EmptyEnum.entries, enumEntries()) + assertEquals(NonEmptyEnum.entries, enumEntries()) + } + + @Test + fun testByCallableReference() { + val empty: () -> EnumEntries = ::enumEntries + assertEquals(EmptyEnum.entries, empty()) + val nonEmpty: () -> EnumEntries = ::enumEntries + assertEquals(NonEmptyEnum.entries, nonEmpty()) + } + + @Test + @Suppress("EnumValuesSoftDeprecate") // For test to avoid comparing entries with entries + fun testSanity() { + compare(EnumEntriesListTest.EmptyEnum.values().toList(), enumEntries()) { listBehavior() } + compare(EnumEntriesListTest.NonEmptyEnum.values().toList(), enumEntries()) { listBehavior() } + } +}