无法在不同的线程池上移动IO

问题描述 投票:1回答:1

我正在阅读关于猫效应的本教程

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

根据这个教程,我编写了这段代码

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)

我得到错误value shift is not a member of cats.effect.IO[String]

教程如何能够移动readLines函数的结果

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

这个样本的最终全球性是我的BlockingIO池上发生了readLine块。

scala scala-cats
1个回答
1
投票

好。我自己找到了答案。我觉得这个教程有点过时了

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"
© www.soinside.com 2019 - 2024. All rights reserved.