FunctionalToDoListWithCats
FunctionalToDoListWithCats copied to clipboard
Question about the implementation of `IOFunctions.writeFile`
Hey Alvin,
First of all, thanks for sharing this project on your GitHub account :)
I've been having a look at the code and I was wondering whether we really need that extra layer of IO
surrounding the IO.bracket
operation. If we remove the call to .unsafeRunSync()
we get just this:
def writeFile(filename: String, text: String, append: Boolean): IO[Unit] = {
// acquire
IO(new BufferedWriter(new FileWriter(new File(filename), append))).bracket { bw =>
// use
IO(bw.write(text + "\n"))
} { bw =>
// release (note that you can put whatever logic you want here, such as logging)
IO(bw.close())
}
}
I made that change, added a couple of to-do items, closed the app and then reopened it and the to-do items were all still there.
So, having in mind that I'm still pretty a rookie with Cats and Cats Effect 🙃, is there any reason why you decided to implement the IOFunctions.writeFile
like that?
Thanks for your time 😎!
Apologies, I just had a look at the accompanying blog post where you state:
At the moment I don’t know why the writeFile function requires a call to unsafeRunSync(), but I can confirm that it doesn’t do anything unless that’s called.
I'm going to try to test it using Graal, because it seems to be only difference between what I'm doing and what I imagine you're doing. I've just been testing it using sbt run
.