Hi all, i have seen some discussions about `Closea...
# stdlib
u
Hi all, i have seen some discussions about
Closeable.use(block)
being suboptimal regarding multiple closeables at a time. Therfore I have implemented a `CloseableScope`which can add closeables to a
List
like this:
Copy code
try {
            CloseableScope {
                val noExceptionCloseable1 = NoExceptionCloseable1().autoClose()
                val bodyExceptionCloseable = BodyExceptionCloseable().autoClose()
                val closeExceptionCloseable1 = CloseExceptionCloseable1().autoClose()
                val closeExceptionCloseable2 = CloseExceptionCloseable2().autoClose()
                val noExceptionCloseable2 = NoExceptionCloseable2().autoClose()

                noExceptionCloseable1.doSomething()
                bodyExceptionCloseable.doSomething()
                closeExceptionCloseable1.doSomething()
                closeExceptionCloseable2.doSomething()
                noExceptionCloseable2.doSomething()
            }
        } catch (t: Throwable) {
            println(t)
        }
CloseableScope(block)
opens the scope,
Closeable.autoClose()
adds a
Closeable
to the cleanup list and at the end of the scope, all Closeables on the list are closed. The multi-exception semantics are modelled after java's try-with-ressource