【发布时间】:2011-10-16 19:38:21
【问题描述】:
当我尝试编译这个小例子时:
trait Foo[A,B] {
type F[_,_]
def foo(): F[A,B]
}
class Bar[A,B] extends Foo[A,B] {
type F[D,E] = Bar[D,E]
def foo() = this
}
object Helper {
def callFoo[A,B,FF <: Foo[A,B]]( f: FF ): FF#F[A,B] =
f.foo()
}
object Run extends App {
val x = new Bar[Int,Double]
val y = Helper.callFoo(x)
println( y.getClass )
}
我得到错误:
[error] src/Issue.scala:20: inferred type arguments
[Nothing,Nothing,issue.Bar[Int,Double]] do not conform to method callFoo's type
parameter bounds [A,B,FF <: issue.Foo[A,B]]
[error] val y = Helper.callFoo(x)
显然,类型推断机制无法从 Bar[A,B] 推断出 A 和 B。但是,如果我手动传递所有类型,它就可以工作:
val y = Helper.callFoo[Int,Double,Bar[Int,Double]](x)
我有办法避免显式传递类型吗?
【问题讨论】:
标签: scala types type-inference