```Hi, I have a question about bracket <https://a...
# arrow
f
Copy code
Hi, I have a question about bracket

<https://arrow-kt.io/docs/0.10/fx/typeclasses/bracket>
Now, let's say we want to open a file, do some work with it, and then close it. With Bracket, we could make that process look like this: val safeComputation = openFile("data.json") .bracket( release = { file -> closeFile(file) }, use = { file -> fileToString(file) }) This would ensure the file gets closed right after completing the use operation, which would be fileToString(file) here. If that operation throws an error, the file would also be closed.
Copy code
I understand this, but what if the risky thing we were doing was not in the fileToString() but also in openFile? Eg if the underlying file library throws an exception because the file doesn't exist?
s
from the looks of it method
openFile(...)
should return a file handle. So, I am guessing if
openFile
throws, then it should most certainly mean that the file handle will not be open, thus the resource management is not needed. That means it’s a simple side effect handling. And that would mean that it should be possible to move
openFile(..)
like this :
Copy code
IO { openFile("data.json") }
.bracket(
  release = { file -> closeFile(file) },
  use = { file -> fileToString(file) }
)
👍 3
p
you can follow up with
handleErrorWith
if you’d like to recover
☝️ 1
f
👍 big thanks!