Scala,高级泛型扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4285510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala, advanced generic extending
提问by v6ak
I'm trying to rewrite https://gist.github.com/319827to Scala. But I can't compile it. What is the correct syntax?
我正在尝试将https://gist.github.com/319827重写为 Scala。但我无法编译它。什么是正确的语法?
Error I'm allways getting:
我总是收到错误:
class type required but java.util.Comparator[_ >: java.lang.Comparable[java.lang.Object]] found
需要类类型,但找到 java.util.Comparator[_>: java.lang.Comparable[java.lang.Object]]
source:
来源:
package v6ak.util
import java.util.Comparator
object NaturalComparator extends Comparator[_ >: Comparable[Object]]{
override def compare(o1:Comparable[Object], o2:Comparable[Object]) = {
if( o1==null || o2==null ){
throw new NullPointerException("Comparing null values is not supported!");
}
o1.compareTo(o2);
}
}
采纳答案by v6ak
I've returned to the problem with more experience and solved it, although I think that is can be better.
我以更多的经验回到了这个问题并解决了它,尽管我认为这可能会更好。
package v6ak.util
import java.util.Comparator
object NaturalComparator extends Comparator[Comparable[Any]]{
def apply[T]() = asInstanceOf[Comparator[T]]
override def compare(o1:Comparable[Any], o2:Comparable[Any]) = {
if( o1 == null || o2 == null ){
throw new NullPointerException("Comparing null values is not supported!")
}
o1 compareTo o2
}
}
回答by shellholic
A extends Bis written A<:Bin scala not A>:B
A extends B是用A<:BScala写的,而不是A>:B
by the way, scala type system is powerful enough to avoid use of Object (AnyRef in scala) in your code
顺便说一句,scala 类型系统足够强大,可以避免在代码中使用 Object(scala 中的 AnyRef)
package v6ak.util
import java.util.Comparator
class NaturalComparator[T <: Comparable[T]] extends Comparator[T] {
override def compare(o1: T, o2: T) = {
if (o1 == null || o2 == null) {
throw new NullPointerException("Comparing null values is not supported!");
}
o1.compareTo(o2);
}
}
object StringComparator extends NaturalComparator[String]
object Examples {
StringComparator.compare("a", "b")
StringComparator.compare(2, "b") // error
}
回答by Mirek Pluta
Well, you made some mess with that java version. Notice that you create an instance of Comparator< Comparable< Object>> and you assign it to value with wildcard - what for ? You won't assign anything else to that variable. Not talking that your getInstance also defines wildecards, while it returns everything the same Comparator< Comparable< Object>>
好吧,你把那个 Java 版本弄得一团糟。请注意,您创建了一个 Comparator< Comparable< Object>> 的实例,并使用通配符将其分配给值 - 有什么用?您不会为该变量分配任何其他内容。不是说您的 getInstance 也定义了通配符,而它返回的所有内容都是相同的 Comparator< Comparable< Object>>
So:
所以:
object NaturalComparator extends Comparator[Comparable[Object]]{
override def compare(o1:Comparable[Object], o2:Comparable[Object]) = {
if(o1 == null || o2 == null){
throw new NullPointerException("Comparing null values is not supported!");
}
o1.compareTo(o2);
}
}

