useAll

inline fun <T> useAll(vararg resources: AutoCloseable, block: () -> T): T

Runs block and then closes every resource in resources in reverse order.

The multi-resource counterpart of kotlin.use. Reverse order means the resource listed last, which is typically the one layered on top of the others, is closed first. Every resource is closed exactly once regardless of how many closes fail.

Failure handling uses Throwable.addSuppressed, which common Kotlin provides on every target:

  • When block throws, that exception is rethrown and every close failure is attached to it as a suppressed exception.

  • When block succeeds but closing fails, the first close failure (in the reverse close order) is thrown after all remaining resources have been closed, with any later close failures attached to it as suppressed exceptions.

Note that on platforms whose suppressed-exception support is a stdlib emulation the suppressed list is still populated; no failure is ever silently dropped.

useAll(source, buffer, cipher) { cipher.process(buffer.read(source)) }

Return

the value produced by block.

Parameters

resources

the resources to close after block completes.

block

the work to perform while all resources are open.


inline fun <T> Iterable<AutoCloseable>.useAll(block: () -> T): T

Runs block and then closes every resource in this collection in reverse iteration order.

Behaves exactly like the vararg useAll overload, including its reverse close order and its suppression rules; see that overload for the details.

Return

the value produced by block.

Parameters

block

the work to perform while all resources are open.