1

I'm using cats-effect 0.10.1 and found out that there is no Bracket typeclass in this version. So this seems I need to introduce some workaround for that.

I'm designing function for Reading from some source (side-effect). So I have \

trait DataFormat

object StreamData{

  def apply[T](path: String)(f: (DataFormat, ReadableByteChannel) => T): IO[T] = 
  IO {
     val channel: ReadableByteChannel = Files.newByteChannel(path)
     try{
        val dataFormat: DataFormat = //...
        //in case of GZIP format open GZIPInputStream
        f(dataFormat, channel)
     } finally {
        if(channel != null) channel.close()
     }
  }
}

The problem with this is that f: (DataFormat, ReadableByteChannel) => T is impure. So I'd make it as f: (DataFormat, ReadableByteChannel) => IO[T] which makes resource releasing in the example above not possible (because I need to flatMap in this case).

Can you suggest any workarounds about it and yet keeping functions pure?

Some Name
  • 8,555
  • 5
  • 27
  • 77

0 Answers0