use

inline suspend fun <T : KAutoCloseable, R> T.use(crossinline block: suspend (T) -> R): R

Executes the given block function on this resource and then closes it.

Return

the result of the block function

Parameters

T

the type of the resource

R

the return type of the block function

block

a function to process this resource

Throws

if an exception occurs during execution or closing


inline suspend fun <T : KAutoCloseable, R> T.use(dispatcher: CoroutineDispatcher, crossinline block: suspend (T) -> R): R

Executes the given block function on this resource within the provided dispatcher context and then closes it.

Return

the result of the block function

Parameters

T

the type of the resource

R

the return type of the block function

dispatcher

the coroutine dispatcher to run the block function

block

a suspend function to process this resource

Throws

if an exception occurs during execution or closing