Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.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
Generics scala:如何创建一个泛型类型,它是scala中所有数字类的子类型,以便它可以包含compare方法_Generics_Scala - Fatal编程技术网

Generics scala:如何创建一个泛型类型,它是scala中所有数字类的子类型,以便它可以包含compare方法

Generics scala:如何创建一个泛型类型,它是scala中所有数字类的子类型,以便它可以包含compare方法,generics,scala,Generics,Scala,我试图创建一个泛型类,泛型类型是Numeric的子类(以确保我处理的是数字) 我试过“classnumivator[TScala的Numeric[T]使用。说classnumivator[T更好,使用classnumivator[T:Numeric] 这也称为上下文绑定,只是类numvector[T](隐式n:Numeric[T]) 其中,n实际上是一个综合生成的名称,您无法直接访问它您使用的Scala2.8的确切版本是什么?这与票证2274有关吗?()这一点的快捷方式是Scala2.8中引入的

我试图创建一个泛型类,泛型类型是Numeric的子类(以确保我处理的是数字)
我试过“
classnumivator[TScala的
Numeric[T]
使用。说
classnumivator[T更好,使用
classnumivator[T:Numeric]

这也称为上下文绑定,只是
类numvector[T](隐式n:Numeric[T])


其中,
n
实际上是一个综合生成的名称,您无法直接访问它

您使用的Scala2.8的确切版本是什么?这与票证2274有关吗?()这一点的快捷方式是Scala2.8中引入的“上下文绑定”:
类NuVector[t:Numeric]
。如果此
T
在范围内,则可以使用
val num=隐式[Numeric[T]]
获取证据参数。
class NuVector[T<:Numeric[T]) extends PartiallyOrdered[T]
{

 /** Array that stores the vector values.
     */
    val v = new Array [T] (_length)
    /** Range for the storage array.
     */
    private val range = 0 to _length - 1

    def compare(x:T,y:T)(implicit res:Numeric[T]) :Int=
    { 
       res.compare(x,y) 
    } 

   def tryCompareTo [B >: NuVector [T]] (b: B)
        (implicit view$1: (B) => PartiallyOrdered [B]): Option [Int] =
    { 
        compare(x,y) 
    } 

    implicit def castT2Ordering(x:T):Numeric[T]=x.asInstanceOf[Numeric[T]]
    implicit def castB2NuVector [B>:NuVector[T]] (b:B): NuVector[T]=
    {
              b.asInstanceOf[NuVector[T]]
    }

}
 could not find implicit value for parameter res:Numeric[T]