put

fun <T : Any> put(key: KClass<T>, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, provider: () -> T)

Registers a lazy singleton provider for the given class key.

The provider lambda will be invoked only the first time a value is requested for this key. The resulting instance is then stored and returned for all subsequent requests.

By default, it is an error to register the same key more than once. This behavior can be changed by setting allowReregister to true in the constructor, in which case subsequent registrations will overwrite previous ones.

The multi-thread behavior depends on threadSafetyMode.

Parameters

key

The KClass to associate with the provider.

threadSafetyMode

The thread safety mode for the lazy initialization.

provider

A lambda that creates the service instance.


inline fun <T : Any> put(threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, noinline provider: () -> T)

Registers a lazy singleton provider for the reified type T.

The provider lambda will be invoked only the first time a value is requested for this key. The resulting instance is then stored and returned for all subsequent requests.

By default, it is an error to register the same key more than once. This behavior can be changed by setting allowReregister to true in the constructor, in which case subsequent registrations will overwrite previous ones.

The multi-thread behavior depends on threadSafetyMode.

This is a convenience function that is equivalent to calling put with T::class as the key.

Parameters

T

The service type to register.

threadSafetyMode

The thread safety mode for the lazy initialization.

provider

A lambda that creates the service instance.