【发布时间】:2018-08-22 19:26:08
【问题描述】:
trait IO[F[_], +A]
case class Pure[F[_], +A](get: A) extends IO[F,A]
case class Request[F[_], I, +A](expr: F[I], receive: I => IO[F,A]) extends IO[F,A]
trait Console[A]
case object ReadLine extends Console[Option[String]]
case class PrintLine(s: String) extends Console[Unit]
trait Run[F[_]] {
def apply[A](expr: F[A]): (A, Run[F])
}
object IO {
@annotation.tailrec
def run[F[_],A](R: Run[F])(io: IO[F,A]): A = io match {
case Pure(a) => a
case Request(expr,recv) =>
R(expr) match { case (e,r2) => println(e.getClass); run(r2)(recv(e)) }
}
}
代码来自“Scala 中的函数式编程”,IDE 抱怨模式匹配中的“recv”应该只接收 Nothing 类型作为其参数,但实际上类型是 Any。但是,它仍然通过了编译。我还认为 recv 将被推断为 Run[F[?], A] 函数的 Request[F[?], Nothing, A] 。这里发生了什么?看起来 Scala 有一些动态特性。它可以推断出运行时中的 I 类型是不正确的。
【问题讨论】:
-
你的 IDE IntelliJ 有机会吗?它对标记不存在的类型错误有一点诀窍。
-
@sepp2k 是的,我现在有一个更明确的问题:stackoverflow.com/questions/51974633/…
标签: scala