fs2 Stream scala 没有类型的隐式。Stream.Compiler[Eval,G_]。

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

我试图创建一个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

我似乎不能得到这个错误? 我错过了什么?这个错误指的是什么?

scala eval scala-cats fs2
1个回答
3
投票

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