Scala丢失了通用类型的参数

时间:2012-05-14 16:47:10

标签: scala types covariance

处理在继承特征的重写函数中使用的棘手的协变类型。基本问题是,[?]类型是什么?我找不到一个好的定义(它有点不可思议),因此不清楚为什么[T]会被[?]替换为以下示例:

> sealed trait Bar[+T]
> trait FooT { type Other; def foo[T,V](stuff:Bar[T]*) { stuff.headOption.isDefined } }
> trait TooF extends FooT { override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff) } }
<console>:7: error: type mismatch;
found   : Bar[T]*
required: Bar[?]
   trait TooF extends FooT { override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff) } }

1 个答案:

答案 0 :(得分:3)

我不确定它显示Bar[?]的确切原因,但我认为它可能类似于类型参数尚未解决。真正的问题是你将varargs传递给super方法的语法是不正确的。它应该是

override def foo[T,V](stuff:Bar[T]*) { super.foo(stuff:_*) }
相关问题