我想做如下的事情:
class A[T] (_arr: Array[T]) { var arr = _arr }
class C {
def foo[T <: A[_]](a: Array[T]){
var ele1 = a(0)
var ele2 = a(1)
for(i <- 0 until ele1.arr.length) {
val temp = ele1.arr(i)
ele1.arr(i) = ele2.arr(i)
ele2.arr(i) = temp
}
}
}
但我收到以下编译器错误:
<console>:15: error: type mismatch;
found : temp.type (with underlying type Any)
required: _$1
ele2.arr(i) = temp
^
我不确定为什么 ele1.arr(i) = ele2.arr(i) 会起作用而 ele2.arr(i) = temp 不会。
编辑:根据雷克斯的建议,我将其更改为以下内容:
class A[T] (_arr: Array[T]) { var arr = _arr }
class C {
def foo[T, B <: A[T]](b: Array[B]){
var ele1 = b(0)
var ele2 = b(1)
for(i <- 0 until ele1.arr.length) {
val temp = ele1.arr(i)
ele1.arr(i) = ele2.arr(i)
ele2.arr(i) = temp
}
}
}
现在编译得很好。但是当我做一个测试用例时,如下:
object Test extends App {
var p = Array.ofDim[A[Int]](2)
var g1 = new A[Int](Array(1, 2, 3, 4, 5))
var g2 = new A[Int](Array(6, 7, 8, 9, 10))
p(0) = g1
p(1) = g2
(new C).foo(p)
p.foreach{ x => println(x.arr.deep.mkString(",")) }
}
我收到以下编译器错误:
src/Test.scala:22: error: inferred type arguments [A[Int],Nothing] do not conform to method foo's type parameter bounds [B <: A[T],T]
(new C).foo(p)
^
src/Test.scala:22: error: type mismatch;
found : Array[A[Int]]
required: Array[B]
(new C).foo(p)
^
two errors found
我知道我可以做 (new C).fooInt, A[Int],但我真的很想让编译器推断它。否则,它有点违背了我正在做的很多事情的目的。