我试图创建一个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
我似乎不能得到这个错误? 我错过了什么?这个错误指的是什么?
Fs2 Stream.Compiler is not found (could not find implicit value Compiler[[x]F[x],G])
Fs2
Stream#compile
现在需要Sync[F]
Eval
的实例。Sync
, IO
确实如此。
试试
def evalString: IO[String] = {
val r = new scala.util.Random(31)
Sync[IO].delay(r.nextString(4))
}
Stream
.eval(evalString)
.repeat
.compile
.lastOrError
.start
.as(ExitCode.Success)