泛型错误:更具体的类型不符合更通用的类型

时间:2015-09-02 17:04:08

标签: scala generics

我有以下代码片段:

private class SectionConversionMap
  extends HashMap[SectionSchema[_], ( Iterable[HtmlRow] ) => Option[Iterable[_]]] {
private type Value[Vt] = ( Iterable[HtmlRow] ) => Option[Iterable[Vt]]
private type SuperKV = (SectionSchema[_], Value[_])

def +[T](kv: (SectionSchema[T], Value[T])):
HashMap[SectionSchema[_], ( Iterable[HtmlRow] ) => Option[Iterable[_]]] = {
  super.+(kv: SuperKV)
}
def get[T](key: SectionSchema[T]): Option[Value[T]] = {
  super.get(key) match {
    case v: Some[Value[T]] => v
    case _ => None
  }
}
}

问题是我的IDE说(在+方法的正文中,kv: SuperKVExpression of type (SectionSchema[T], SectionConversionMap.this.Value[T]) does not conform to expected type (SectionSchema[_], SectionConversionMap.this.Value[_])

怎么可能?第二个更通用。

1 个答案:

答案 0 :(得分:1)

下划线并不等同于"?"在Java中,虽然大多数时候它可以以相同的方式使用。 " _"是用于指定更高级的类型,而且看起来或多或少似乎是你在这里尝试做的事情,尽管我认为你的代码可以通过对类型定义方式的一些清理来做。

尽管如此,您可以使用将Map替换为HashMap的简单方法来编译代码。这是你的疏忽吗?我看不出你为什么要尝试扩展HashMap而不是Map的原因(我的编译器实际上也发出了关于它的警告)。

相关问题