【问题标题】:fs2 Stream scala No implicit of type : Stream.Compiler[Eval,G_]fs2 Stream scala 没有隐式类型:Stream.Compiler[Eval,G_]
【发布时间】: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


【解决方案1】:

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)

【讨论】:

猜你喜欢
  • 2020-11-03
  • 1970-01-01
  • 2020-08-09
  • 2020-02-11
  • 2018-11-21
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 2021-10-19
相关资源
最近更新 更多