0

我正在尝试创建一个 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

我似乎无法得到错误?我错过了什么?错误指的是什么?

4

1 回答 1

3

Fs2 Stream.Compiler 未找到(找不到隐含值 Compiler[[x]F[x],G])

Fs2Stream#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)
于 2020-06-10T10:13:01.133 回答