参数 P 的隐含值在哪里:cats.Parallel[cats.effect.IO,F]

Where is the implicit value for parameter P: cats.Parallel[cats.effect.IO,F]

运行 Cats Effect 文档中子主题 parSequence 下的示例代码片段引发错误,

import cats._, cats.data._, cats.syntax.all._, cats.effect.IO

val anIO = IO(1)

val aLotOfIOs = NonEmptyList.of(anIO, anIO)
val ioOfList = aLotOfIOs.parSequence

<console>:44: error: could not find implicit value for parameter P: cats.Parallel[cats.effect.IO,F]

我包括隐式 Timer[IO]implicit val timer = IO.timer(ExecutionContext.global) 但它 工作。请指教。谢谢

更新 #1

对于完整的工作片段,

import cats._, cats.data._, cats.syntax.all._, cats.effect.IO
import scala.concurrent.ExecutionContext.Implicits.global

implicit val contextShift = IO.contextShift(global)

val anIO = IO(1)
val aLotOfIOs = NonEmptyList.of(anIO, anIO)
val ioOfList = aLotOfIOs.parSequence

您要查找的隐式在 cats.effect.IOInstances 中定义,您可以通过导入 cats.effect.IO._.

将其纳入范围
private[effect] abstract class IOInstances extends IOLowPriorityInstances {
  //....

    implicit def ioParallel(implicit cs: ContextShift[IO]): Parallel[IO, IO.Par] =
    new Parallel[IO, IO.Par] {
      final override val applicative: Applicative[IO.Par] =
        parApplicative(cs)
      final override val monad: Monad[IO] =
        ioConcurrentEffect(cs)

      final override val sequential: ~>[IO.Par, IO] =
        new FunctionK[IO.Par, IO] { def apply[A](fa: IO.Par[A]): IO[A] = IO.Par.unwrap(fa) }
      final override val parallel: ~>[IO, IO.Par] =
        new FunctionK[IO, IO.Par] { def apply[A](fa: IO[A]): IO.Par[A] = IO.Par(fa) }
    }
}

object IO extends IOInstances {
  // ...
}

请注意,如果要使用 ioParallel 实例,则需要在范围内有一个隐式 ContextShift[IO]

Scala 中的一种常见模式是将隐式实例定义为 class(在本例中为 IO)的伴随对象的一部分。