getOrProvide

fun <T : Any, GetParams, PutParams> getOrProvide(key: ServiceKey<T, *, GetParams, PutParams>, allowedScopes: (S) -> Boolean, putParams: PutParams, getParams: GetParams): T

Fetches an item for the given key.

If an entry for the key has not been previously registered, this function determines how to create a new one. Creation is subject to the allowedScopes predicate.

  • If the scope is allowed, a new entry is created using the provided putParams.

  • If the scope is not allowed, it delegates to onInvalidScope to create a fallback entry.

Parameters

key

The ServiceKey for the desired service.

allowedScopes

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

putParams

The parameters needed to create a new ServiceEntry for key if one doesn't exist.

getParams

The parameters needed to retrieve the value from the entry.


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

Fetches an item for the given 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.

This is a convenience function equivalent to calling getOrProvide with classKey<T>().

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.