【发布时间】:2020-06-10 18:22:23
【问题描述】:
我正在尝试按如下方式创建 Stream[Eval, String]:
import cats.Eval
import cats.effect.{ExitCode, IO, IOApp}
import fs2._
object StringEval extends IOApp {
def evalString: Eval[String] = Eval.always{
val r = new scala.util.Random(31)
r.nextString(4)
}
override def run(args: List[String]): IO[ExitCode] = {
Stream
.eval(evalString)
.repeat
.compile
.lastOrError
.start
.as(ExitCode.Success)
}
}
但问题是我收到一个编译错误:
Error:(17, 8) could not find implicit value for parameter compiler: fs2.Stream.Compiler[[x]cats.Eval[x],G]
.compile
我似乎无法得到错误?我错过了什么?错误指的是什么?
【问题讨论】:
-
我认为您应该避免使用
Eval数据类型来捕获副作用,因为没有处理错误的能力(没有MonadError[Eval]的实例)。也许Sync数据类型是解决您问题的好选择。 -
那只是为了测试,IO做的事情,所以程序的组织方式没有问题?
标签: scala eval scala-cats fs2