4

I want to create a directory, then do something with the directory and finally delete it. I'm using a bracket idiom for that.

val fs: FileSystem = ???
val path = ???
ZIO.bracket[Any, Throwable, Path, Unit](
    acquire = ZIO{fs.mkdirs(path); path},
    release = p => ZIO.succeed(fs.delete(p, true)),
    use = p => ZIO{()})

Deleting a directory is apparently an error-prone action. But release function has to always succeed. So I have to use ZIO.succeed which looks wrong.

How to close the resource properly?

simpadjo
  • 3,947
  • 1
  • 13
  • 38

1 Answers1

2

Based on the discussion on gitter: https://gitter.im/ZIO/Core?at=5d44552a7a151629e10f68a3

release can't return error because otherwise it would be impossible to distinguish between an error from release and an error from use.

Possible solutions:

1) release = ZIO.succeed(action) - turns an error into unrecoverable error. When release is safe or indeed unrecoverable.

2) release = Task{action}.ignore - ignores potential error.

3) make error a value. So Zio.bracket will be of type ZIO[R, E, Either[FinalizerError, Good]]. When recovery is really important.

1 and 2 are meant to cover vast majority of usecases.

simpadjo
  • 3,947
  • 1
  • 13
  • 38