【发布时间】:2009-08-31 14:25:12
【问题描述】:
我正在尝试创建一个泛型类,泛型类型是 Numeric 的子类(以确保我正在处理数字。)
我尝试将“class NuVector[T<:Numeric[T])”作为类 def 并且编译正常。
现在我想给它添加PartiallyOrdered[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]
我使用的 Scala 版本是 2.8
非常感谢任何帮助。
谢谢,
〜老虎。
我不知道这是一个错误还是我的定义有问题。
【问题讨论】:
-
您使用的是哪个版本的 Scala2.8?这与票证 2274 有关吗? (lampsvn.epfl.ch/trac/scala/ticket/2274)