What is the `EnumEntries<E>` type, what guarantees does it provide, and how does the compiler back the `entries` property?
answer
- Type: kotlin.enums.EnumEntries<E> : List<E>
- Immutable, declaration order, cached single instance
- Compiler synthesizes the entries property per enum
- entries === entries (identity stable)
- Generic builder: enumEntries<T>()
basics
~10 sEnumEntries<E> is a special read-only list type that holds all of an enum's constants. The compiler generates the entries property to return one shared, unchangeable instance for the enum.
solid answer
~40 s`entries` has the type `kotlin.enums.EnumEntries<E>`, an interface that **extends `List<E>`**. It is a specialized, immutable list of an enum's constants designed so the runtime can cache a single instance. The compiler synthesizes the `entries` property on every `enum class`, backing it with this cached `EnumEntries` (often built from the same constant data underlying `values()`). Guarantees: full `List<E>` read API, declaration order, immutability, and identity stability across calls (`MyEnum.entries === MyEnum.entries`). You don't construct it yourself for your own enums; for generic code the standard library exposes `enumEntries<T>()` (and an internal `enumEntries(values)` builder the compiler uses). Because it's just a `List`, it composes with all collection operators and needs no array conversion.
code
kotlin · 5 linesenum class Planet { MERCURY, VENUS, EARTH }
val entries: kotlin.enums.EnumEntries<Planet> = Planet.entries
val isList: Boolean = entries is List<Planet> // true
val cached: Boolean = Planet.entries === Planet.entries // truego deeper
Knows entries is some read-only list of the enum's constants.
Identifies the List<E> surface and immutability, uses it like any list.
Names kotlin.enums.EnumEntries<E> : List<E>, the synthesized property, caching, and identity stability.
Explains the rationale for a dedicated cached type and its interplay with the compiler-synthesized enum members and generic builders.
## The type `MyEnum.entries` is of type **`kotlin.enums.EnumEntries<MyEnum>`**. `EnumEntries<E>` is an **interface that extends `List<E>`** (specifically a read-only, random-access list). It lives in the `kotlin.enums` package and was stabilized with the `entries` feature in **Kotlin 1.9**. ## Guarantees it provides - **Read-only `List<E>` surface**: `size`, `get`/indexing, `iterator()`, `contains`, `indexOf`, and all extension operators (`map`, `filter`, `associateBy`, …). - **Declaration order**: identical to `values()`. - **Immutability**: no `MutableList` operations; you cannot add/remove/replace. - **Identity stability**: the same cached instance is returned every time, so `a.entries === a.entries`. This is what enables the zero-allocation property. ## How the compiler backs it For any `enum class`, the compiler **synthesizes** the `entries` property (alongside the long-standing synthetic `values()` and `valueOf()`). It is backed by a **single cached `EnumEntries`** instance created from the enum's constant array. Conceptually: ```kotlin // Conceptual — what the compiler arranges for you (do not write this yourself): enum class Planet { MERCURY, VENUS, EARTH } // Planet.entries : EnumEntries<Planet> (cached, immutable, == across calls) ``` For **generic** code, the standard library provides the public `enumEntries<T>()` (reified) and an internal builder the compiler uses to wrap the values array into an `EnumEntries`. As an application developer you never instantiate `EnumEntries` directly for your own enums — you just read `MyEnum.entries`. ## Why a dedicated type instead of `List<E>` directly - It signals intent (an enum's constant set) and lets the compiler/runtime guarantee caching and immutability. - Being a `List<E>`, it stays fully interoperable with the collections API — no conversion friction. ```kotlin enum class Planet { MERCURY, VENUS, EARTH } val byName: Map<String, Planet> = Planet.entries.associateBy { it.name } val third: Planet = Planet.entries[2] // EARTH (random access) val same = Planet.entries === Planet.entries // true: cached instance ``` ## Practical implications - Treat it exactly like an immutable `List<E>`. - Don't try to mutate or downcast it to `MutableList`. - Prefer it everywhere you used `values()` for read-only access; convert with `toTypedArray()` only at array boundaries.
- Is `EnumEntries<E>` mutable or downcastable to `MutableList`?No — it's an immutable read-only `List<E>`; attempting to use it as a `MutableList` would fail (and is a code smell).
- Do you ever construct an `EnumEntries` yourself?Not for your own enums — the compiler synthesizes and caches it. Generic code reads it via `enumEntries<T>()`.
EnumEntries is a sealed display case of the enum's constants: you can read and index every item, but the case never opens to add or swap one.
saying these in an interview costs you the question
- Claiming `entries` returns a plain `Array` or a `MutableList`.
- Saying you must manually construct `EnumEntries`.
- Not knowing `EnumEntries<E>` extends `List<E>`.
- Asserting each access returns a new instance.
- Confusing it with `Map.Entry`/entries of a map.