相关疑难解决方法(0)

为什么类型参数绑定T <:Comparable [T]为T = Int失败?

scala> class Foo[T <: Comparable[T]](val x : T)
defined class Foo

scala> (3: Int).asInstanceOf[Comparable[Int]]  
res60: java.lang.Comparable[Int] = 3

scala> new Foo(3)                              
<console>:13: error: inferred type arguments [Int] do not conform to class Foo's type parameter bounds [T <: java.lang.Comparable[T]]
       new Foo(3)
       ^
Run Code Online (Sandbox Code Playgroud)

第二个表达式是类型擦除的结果吗?

我将如何定义Foo以便我可以使用Int对其进行参数化,但仍然能够使用其实例变量执行某些排序行为?

java scala scala-2.8

13
推荐指数
3
解决办法
2391
查看次数

标签 统计

java ×1

scala ×1

scala-2.8 ×1