Fred Friis
05/28/2020, 12:00 AMHi, 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.
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?
Satyam Agarwal
05/28/2020, 6:35 AMopenFile(...)
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 :
IO { openFile("data.json") }
.bracket(
release = { file -> closeFile(file) },
use = { file -> fileToString(file) }
)
pakoito
05/28/2020, 10:08 AMhandleErrorWith
if you’d like to recoverFred Friis
05/28/2020, 11:13 PM