【发布时间】:2017-02-18 14:59:16
【问题描述】:
我正在将 servant-server 端口连接到 Scala。这个想法是使用类型类解析来归纳构建可以处理请求的函数。我遇到了一些我无法弄清楚的奇怪推理问题。
object Servant {
class :>[Path, A]
trait HasServer[A] {
type ServerT
def route(a: ServerT): String
}
implicit val unitServer = new HasServer[Unit] {
type ServerT = String
def route(str: ServerT): String = str
}
implicit def subServer[A, Sub](implicit sub: HasServer[Sub]) = new HasServer[A :> Sub] {
type ServerT = A => sub.ServerT
def route(handler: ServerT): String = "handler"
}
}
通过上述,以下编译失败:
val foo = implicitly[HasServer[Int :> Unit]]
implicitly[=:=[Int => String, foo.ServerT]]
错误是:
servant.scala:33: error:
Cannot prove that Int => String =:= Main.$anon.Servant.foo.ServerT.
但是,如果我通过以下方式直接实例化HasServer[Int :> Unit],它将编译:
val foo = new HasServer[Int :> Unit] {
type ServerT = Int => unitServer.ServerT
def route(handler: ServerT): String = handler(10)
}
如何编译?谢谢!
【问题讨论】: