declare

inline fun <T> declare(instance: T, qualifier: Qualifier? = null, secondaryTypes: List<KClass<*>> = emptyList(), allowOverride: Boolean = true, holdInstance: Boolean = false)

Declare an instance definition for the current scope using the given object.

This results in declaring a scoped definition of type T, bound to the provided instance. The instance will be dropped when the scope is closed.

The holdInstance parameter controls whether the instance is retained by Koin or not:

  • holdInstance = true → the instance is held within the scope and behaves like a singleton.

  • holdInstance = false → the instance is not held; the definition exists in current scope, but cannot be resolved in other scope instance.

This is useful for injecting pre-constructed instances into a specific scope.

Parameters

instance

The instance to declare.

qualifier

An optional qualifier to distinguish this binding.

secondaryTypes

A list of additional types this instance should be bound to.

allowOverride

Whether this declaration can override an existing one (default is true).

holdInstance

Whether to retain the instance for future resolution within new scopes, or just hold for current scope (holdInstance = false).