ClassKey

sealed interface ClassKey<T : Any>

A ServiceKey that uses the service's own KClass as its identifier.

This key kind is useful for the common case where you only need a single instance of a service and can identify it by its type alone, without needing to declare an explicit key instance.

All ClassKey implementations for the same service type T are considered equal. This allows, for example, a service to be registered with a LazyClassKey and later retrieved using a SingletonClassKey for the same type.

Warning on Type Erasure: Due to JVM type erasure, it is not advisable to use ClassKey with generic types (e.g., List<String>). The key's identity is based on the erased type (List in this case), meaning ClassKey<List<String>> and ClassKey<List<Int>> would be treated as the same key. For generic services, use explicit LazyKey or SingletonKey instances instead.

Inheritors

Properties

Link copied to clipboard
abstract val type: KClass<T>