`IO::bracket` works well when you have something l...
# arrow
r
IO::bracket
works well when you have something like this:
Copy code
IO { take a resource safely }.bracket(release = { give it back }, use = { use it in a way that can fail })
but what about the case where taking the resource can also fail? Is there a method that can handle this, or do I need another
bracket
?
j
I always understood failure in resource acquisition to mean that the resource was not acquired, if you need a finalizer for that case that usually means you are acquiring more than one resource, or a resource to initialize another resource in which case you should use another bracket. If you end up nesting brackets you should use the resource datatype to get back some nice composition: https://github.com/arrow-kt/arrow/blob/master/modules/fx/arrow-fx/src/main/kotlin/arrow/fx/Resource.kt
Resource helps with cases in which you do something like: - acquire r1 - acquire r2 - use both - release r2 - release r1 It let's you describe the acquisition in a much cleaner way than bracket
Oh and also if resource aquisition fails the entire IO returned by bracket will fail and the finalizer will not be run afaik, so a simple error handler will do the trick there
s
I always understood failure in resource acquisition to mean that the resource was not acquired
That is correct, we cannot generically figure out what went wrong where. Additionally
bracket
is useful for resource safety across async jumps. When you’re i.e opening a file with cached bounded pool and processing it on an unbounded pool.
Did you have a specific use-case in mind?