【发布时间】:2016-03-04 05:15:38
【问题描述】:
我试过了:
implicit class ArrayExtensions[A](a: Array[A]) {
/**
* Sort a slice [from, until) of this array
*/
def sort(from: Int, until: Int)(implicit cmp: Ordering[A]) = java.util.Arrays.sort(a, from, until, cmp)
}
但是,我想我正在打a bug in the compiler:
[error] found : Array[A]
[error] required: Array[? with Object]
[error] Note: A >: ? with Object, but class Array is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ >: ? with Object`. (SLS 3.2.10)
[error] def sort(from: Int, until: Int)(implicit cmp: Ordering[A]) = java.util.Arrays.sort(a, from, until, cmp)
我该如何解决这个问题?
【问题讨论】:
-
我认为JVM可以是一个标签。我认为 Java 不应该是一个标签。
-
直接调用
java.util.Arrays.sort有什么问题? -
@kostya:我刚刚发布了调用
java.util.Arrays时遇到的编译错误 -
我的意思是,如果你有
val a: Array[Int],你可以打电话给Arrays.sort(a, from, until),它就可以工作。为什么要介绍ArrayExtensions? -
我设法通过复制
scala.util.Sorting.quickSort代码,将其中的Array替换为mutable.IndexedSeq并使用quicksort(a.view(from, until))进行排序来使其工作。请参阅 gist.github.com/kolmar/5b891c6058b16757525b 我不确定为什么 Scala 不提供任何内置的quickSort方法来对可变序列进行就地排序。
标签: scala scala-collections scala-2.11 scala-generics