【发布时间】:2014-11-24 09:25:32
【问题描述】:
在库中,有一个类具有更高种类的类型,它采用一个类型参数。我想给它一个接受两个类型参数的类型,所以我使用type表达式来修复另一个参数。
但结果并不像我预期的那样。
代码简化为:
object Main {
class Bar[T[_]] {
def bar[A]: Option[T[A]] = None
}
def foo[A] = {
type T[B] = Map[A, B]
new Bar[T]
}
val f: Option[Map[String, Int]] = foo[String].bar[Int]
}
编译时出现错误(Scala 2.11.4):
test.scala:12: error: type mismatch;
found : Option[T[Int]]
(which expands to) Option[scala.collection.immutable.Map[A,Int]]
required: Option[Map[String,Int]]
val f: Option[Map[String, Int]] = foo[String].bar[Int]
^
one error found
为什么会出现类型错误?
【问题讨论】:
标签: scala types compiler-errors higher-kinded-types