我试图创建一个泛型类,通用类型是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]]
}
}
Run Code Online (Sandbox Code Playgroud)
它没有编译.我在编译时得到的错误是:
could not find implicit value for parameter res:Numeric[T]
Run Code Online (Sandbox Code Playgroud)
我使用的Scala版本是2.8
任何帮助是极大的赞赏.
谢谢,
〜老虎.
我不知道这是一个错误还是我的定义问题.
更好,使用 class NuVector[T:Numeric]
这也被称为上下文绑定,并且只是语法糖 class NuVector[T](implicit n: Numeric[T])
这里n实际上是一些你不能直接访问综合生成的名称
| 归档时间: |
|
| 查看次数: |
5902 次 |
| 最近记录: |