Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala:如何让编译器从Seq[T]类型的函数参数推断类型?_Scala_Type Inference - Fatal编程技术网

Scala:如何让编译器从Seq[T]类型的函数参数推断类型?

Scala:如何让编译器从Seq[T]类型的函数参数推断类型?,scala,type-inference,Scala,Type Inference,我正在生成一个泛型函数,它对以下类的不同子类进行排序: class SortableByGeographicPoint(val geographicPoint: Int) 其子类如下所示: case class A(id: Int, override val geographicPoint: Int) extends SortableByGeographicPoint(geographicPoint) 我的职能是: def sortByGeoPoint[T <: SortableByG

我正在生成一个泛型函数,它对以下类的不同子类进行排序:

class SortableByGeographicPoint(val geographicPoint: Int)
其子类如下所示:

case class A(id: Int, override val geographicPoint: Int) extends SortableByGeographicPoint(geographicPoint)
我的职能是:

def sortByGeoPoint[T <: SortableByGeographicPoint](sequence: Seq[SortableByGeographicPoint]): Seq[T] = {
   sequence.sortBy(_.geographicPoint) map(_.asInstanceOf[T])
}

def sortByGeoPoint[TOops,我只需要这样做:

  def sortByGeoPoint2[T <: SortableByGeographicPoint](sequence: Seq[SortableByGeographicPoint]): Seq[T] = {
      sequence.sortBy(_.geographicPoint) map(_.asInstanceOf[T])
  }

不需要安装
asInstanceOf
,这是不安全的:

def sortByGeoPoint3[T <: SortableByGeographicPoint](sequence: Seq[T]): Seq[T] = 
  sequence.sortBy(_.geographicPoint)

def sortByGeoPoint3[T为什么不在
Seq
类型参数中使用
T

def sortByGeoPoint[T <: SortableByGeographicPoint](sequence: Seq[T]): Seq[T] =
  sequence.sortBy(_.geographicPoint)
def sortByGeoPoint[T
def sortByGeoPoint3[T <: SortableByGeographicPoint](sequence: Seq[T]): Seq[T] = 
  sequence.sortBy(_.geographicPoint)
def sortByGeoPoint[T <: SortableByGeographicPoint](sequence: Seq[T]): Seq[T] =
  sequence.sortBy(_.geographicPoint)