I currently face the problem of correctly closing resources that never leave their containing Either
.
The relevant code looks something like this:
object SomeError
class MyRes : AutoCloseable { [...] }
fun createRes(): Either<SomeError, MyRes> { [...] }
fun extractData(res: MyRes): String { [...] }
fun theProblem(): Either<SomeError, String> {
return createRes()
.map { extractData(it) }
}
What is the most idiomatic way of closing the created MyRes
? Closing it before that map
prevents extractData
from accessing it, and after the map
I can't access it anymore via Either
's operations. Closing it in extractData
severely limits composability.
Currently I have an external List<AutoCloseable>
that I iterate over after all the computations, but that can't be the intended way.
I am open to using Arrow Fx (e.g. Resource
) if that helps, but I haven't found anything on how to combine Either
and Resource
in an elegant way.