将泛型通配符从Java转换为Scala [英] Translating generic wildcards from Java to Scala

查看:144
本文介绍了将泛型通配符从Java转换为Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.Collections 类中,我们有两个变体 sort 方法,包含相应比较器的任意对象的列表:

  public static< ; T> void sort(List< T> list,Comparator<?super T>比较器)

Comparable 对象列表:

  public static< T extends Comparable< ;? super T>> void sort(List< T> list)






I在想如何将这种方法签名与有界通配符翻译成Scala。对于第一个版本,我直接翻译了签名,并且一见钟情,没有编译问题:

  def sort [T](list:列表[T],比较器:比较器[_>:T]){??? } 

但是我发现我无法用以下参数调用此方法:

  val比较器=新比较器[对象] {
def比较(o1:Object,o2:Object)= ???
}
val list = new ArrayList [Number]
sort [Object](list,comparator)

即使我明确指定类型 T Object ,最后一行会出现此编译错误$ c $。


类型不匹配;找到:java.util.ArrayList [Number] required:java.util.List [Object]注意:Number<:Object,但Java定义的特征List在类型E中不变。您可能希望调查通配符类型,例如 _<:Object 。 (SLS 3.2.10)

实际上,我发现甚至不可能直接调用唯一的Java方法,因为它失败了相同类型的错误。

  Collections.sort [Object](list,comparator)






至于具有类似列表的版本,我想出了这个声明:

  def sort [T<:Comparable [_>:T]](list:List [T]){??? } 

但这完全不起作用:


涉及类型T的非法循环引用






我做错了什么?斯卡拉变种泛型遵循不同规则的Java?如何在不实际发生编译错误的情况下调用 Collections.sort 方法



不,我没有真正问过如何在Scala中对列表进行排序。我知道Scala有自己的集合,排序函数和不同的方法来比较对象(比如 Ordered Ordering T 的错误类型参数:您对 List [Number] 进行排序,而不是 List [b]

$ $ $ $ $ $ $ $ $ $ $ $ code>

会起作用。



如果你想调用没有类型的排序参数,你需要定义两个参数列表(因为类型推理在Scala中的工作原理):

  def sort [T] (列表:列表[T])(比较器:比较器[_>:T]){??? } 

//然后
sort(list)(comparator)

你可能要考虑使用Scala类型,它对协方差有适当的支持(即在Scala中a List [Number] 是一个 List [Object ] )。



关于可比较的版本,您必须明确写出通配符:


$ b

  def sort [T<:Comparable [T],U<:T](list:List [U]){??? } 


In the java.util.Collections class, we have two variants of the sort method, one that takes a list of arbitrary objects with a corresponding Comparator:

public static <T> void sort(List<T> list, Comparator<? super T> comparator)

And one that takes a list of Comparable objects:

public static <T extends Comparable<? super T>> void sort(List<T> list)


I was thinking how one woulds translate such method signatures with bounded wildcards into Scala. For the first version, I translated the signature literally and at first sight without compilation problems:

def sort[T](list: List[T], comparator: Comparator[_ >: T]) { ??? }

But then I found that I could not invoke this method with the following arguments:

val comparator = new Comparator[Object] {
    def compare(o1: Object, o2: Object) = ???
}
val list = new ArrayList[Number]
sort[Object](list, comparator)

The last line gives this compile error, even though I explicitly specify the type T as Object.

type mismatch; found : java.util.ArrayList[Number] required: java.util.List[Object] Note: Number <: Object, but Java-defined trait List is invariant in type E. You may wish to investigate a wildcard type such as _ <: Object. (SLS 3.2.10)

Actually, I found out that it's even impossible to call the sole Java method directly as it fails with the same type of error.

Collections.sort[Object](list, comparator)


As for the version with comparable list, I came up with this declaration:

def sort[T <: Comparable[_ >: T]](list: List[T]) { ??? }

But this doesn't work at all:

illegal cyclic reference involving type T


What am I doing wrong? Are Scala variant generics following different rules the the Java ones? How call one call the Collections.sort method without actually getting a compile error?

Side note:

No, I'm not really asking how I can sort a list in Scala. I know that Scala has its own set of collections, sort functions and a different approach to comparing objects (such as Ordered and Ordering traits). My question concerns the general problem of generic methods and translation of generics from Java to Scala.

解决方案

You are giving the wrong type parameter for T: You sort a List[Number], not a List[Object]:

sort[Number](list, comparator)

will work.

If you want to call sort without a type argument, you need to define two argument lists (because of how type-inference works in Scala):

def sort[T](list: List[T])(comparator: Comparator[_ >: T]) { ??? }

// Then
sort(list)(comparator)

You might want to consider using Scala types, that have proper support for covariance (i.e. in Scala a List[Number] is a List[Object]).

Concerning the version with comparable, you'll have to explicitly write the wildcard:

def sort[T <: Comparable[T], U <: T](list: List[U]) { ??? }

这篇关于将泛型通配符从Java转换为Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆