6

为什么我error: could not find implicit value for parameter C: scalaz.Catchable[F2]在执行时得到以下信息P(1,2,3).run

[scalaz-stream-sandbox]> console
[info] Starting scala interpreter...
[info]
import scalaz.stream._
import scala.concurrent.duration._
P: scalaz.stream.Process.type = scalaz.stream.Process$@7653f01e
Welcome to Scala version 2.11.0-RC3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> P(1,2,3).run
<console>:15: error: could not find implicit value for parameter C: scalaz.Catchable[F2]
              P(1,2,3).run
                       ^

scalaz-stream-sandbox 项目在 GitHub 上可用。执行sbt console然后P(1,2,3).run面对问题。

4

2 回答 2

8

当您编写 时Process(1, 2, 3),您会得到一个Process[Nothing, Int],这是一个对可以发出外部请求的特定上下文没有任何想法的过程——它只会发出一些东西。这意味着您可以将其视为Process0,例如:

scala> Process(1, 2, 3).toList
res0: List[Int] = List(1, 2, 3)

但是,这也意味着您不能run这样做,因为 run 需要“驱动程序”上下文。

由于Process在其第一个类型参数中是协变的,因此您可以在您确实对此上下文具有更具体类型的情况下使用它:

scala> import scalaz.concurrent.Task
import scalaz.concurrent.Task

scala> (Process(1, 2, 3): Process[Task, Int]).runLog.run
res1: IndexedSeq[Int] = Vector(1, 2, 3)

或者:

scala> Process(1, 2, 3).flatMap(i => Process.fill(3)(i)).runLog.run
res2: IndexedSeq[Int] = Vector(1, 1, 1, 2, 2, 2, 3, 3, 3)

我同意该错误有点令人困惑,但在正常使用中,您通常不会遇到这种情况,因为您将在将其类型修复为类似Process[Task, Int].

于 2014-04-08T09:49:43.767 回答
1

在 a Process0[O]like上Process(1, 2, 3),您可以调用.toSource以将其转换为 a Process[Task, O]and runLog.runit,或者您可以直接调用 Functions liketoListtoVector以获取其结果。

于 2015-10-16T13:24:53.650 回答