KT-53154 enumEntries experimental intrinsic stub
Provide an inefficient default implementation and make it "@PublishedApi internal" so it can be safely merged as is. Intrinsics will be added later in follow-up PRs, and then the implementation will be adjusted accordingly Merge-request: KT-MR-10843 Merged-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
This commit is contained in:
committed by
Space Team
parent
0d19a5b3ea
commit
37c2c862d8
@@ -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<E : Enum<E>> : List<E>
|
||||
|
||||
/**
|
||||
* Returns [EnumEntries] list containing all enum entries for the given enum type [T].
|
||||
*/
|
||||
@PublishedApi
|
||||
@ExperimentalStdlibApi
|
||||
@SinceKotlin("1.9")
|
||||
internal inline fun <reified T : Enum<T>> enumEntries(): EnumEntries<T> {
|
||||
/*
|
||||
* 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<T>())
|
||||
}
|
||||
|
||||
@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 <E : Enum<E>> enumEntries(entriesProvider: () -> Array<E>): EnumEntries<E> = EnumEntriesList(entriesProvider())
|
||||
|
||||
@@ -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<EmptyEnum> = ::enumEntries
|
||||
assertEquals(EmptyEnum.entries, empty())
|
||||
val nonEmpty: () -> EnumEntries<NonEmptyEnum> = ::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() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user