Search code examples
scalascala-cats

Not able to shift the IO on a different thread pool


I am reading this tutorial on cats effects

https://typelevel.org/blog/2017/05/02/io-monad-for-cats.html

Based on this tutorial I wrote this code

import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
import cats.effect.IO

val Main = ExecutionContext.global
val BlockingIO = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())

val program = for {
_ <- IO { println("what is your name") }
name <- IO { readLine() }.shift(BlockingIO).shift(Main)
} yield s"Hello $name"
val output = program.unsafeRunSync
println(output)

I get error value shift is not a member of cats.effect.IO[String]

How is the tutorial able to shift the result of the readLines function

lines <- readLines("names.txt").shift(BlockingFileIO).shift(Main)

My ultimate global with this sample is that the block of readLine happens on my BlockingIO pool.


Solution

  • OK. I found the answer myself. I think that tutorial is a little dated

    https://typelevel.org/cats-effect/datatypes/io.html

    val program = for {
      _ <- IO { println("what is your name") }
      _ <- IO.shift(BlockingIO)
      name <- IO { readLine }
      _ <- IO.shift(Main)
    } yield s"Hello $name"