getOrProvide

fun <T : Any> getOrProvide(key: KClass<T>, allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, provider: (S) -> T): T

Fetches the singleton instance for the given class key.

If an instance has not been previously registered, it creates and stores a new one using the provider lambda. Creation is subject to the allowedScopes predicate; if the current scope is not allowed, this delegates to onInvalidScope.

The multi-thread behavior depends on threadSafetyMode.

Parameters

key

The class of the service to retrieve.

allowedScopes

A predicate to check if the current scope is valid for this provider.

threadSafetyMode

The thread safety mode for the lazy initialization.

provider

A lambda that creates the service instance if one doesn't exist.


inline fun <T : Any> getOrProvide(noinline allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, noinline provider: (S) -> T): T

Fetches the singleton instance for the reified type T.

If an instance has not been previously registered, it creates and stores a new one using the provider lambda. Creation is subject to the allowedScopes predicate; if the current scope is not allowed, this delegates to onInvalidScope.

The multi-thread behavior depends on threadSafetyMode.

This is a convenience function equivalent to calling getOrProvide with T::class.

Parameters

T

The service type to retrieve.

allowedScopes

A predicate to check if the current scope is valid for this provider.

threadSafetyMode

The thread safety mode for the lazy initialization.

provider

A lambda that creates the service instance if one doesn't exist.