当类型参数嵌套时,为什么Scala完全不推断类型参数?

请考虑以下Scala代码:

abstract class A
abstract class B[T <: A]
class ConcreteA extends A
class ConcreteB extends B[ConcreteA]

class Example[U <: B[T], T <: A]( resolver: U )
object Test {
    new Example( new ConcreteB )
}

最后一行新示例(新ConcreteB)无法编译,并显示以下错误:

error: inferred type arguments [ConcreteB,Nothing] do not conform to class Example’s type parameter bounds [U <: B[T],T <: A]

但是ConcreteB拥有所有必要的数据来解决U和T.我在这里缺少什么?

Kipton与他更高的解决方案接近.不幸的是,他跳过了Scala的一个bug, 2.9.1.RC1.以下作品与2.9.1.RC1和行李箱预期相符,

Welcome to Scala version 2.9.1.RC1 (Java HotSpot(TM) Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class A
defined class A

scala> abstract class B[T <: A]
defined class B

scala> class ConcreteA extends A
defined class ConcreteA

scala> class ConcreteB[T <: A] extends B[T]
defined class ConcreteB

scala> class Example[T <: A, U[X <: A] <: B[X]](resolver: U[T])
defined class Example

scala> new Example(new ConcreteB[ConcreteA])
res0: Example[ConcreteA,ConcreteB] = Example@ec48e7
翻译自:https://stackoverflow.com/questions/6892781/why-doesnt-scala-fully-infer-type-parameters-when-type-parameters-are-nested

转载注明原文:当类型参数嵌套时,为什么Scala完全不推断类型参数?