好的,所以我有这个:
implicit final class RichIterableLike[A, Repr <: IterableLike[A, Repr]](val it: Repr)
extends AnyVal {
def pairDiff[To](implicit num: Numeric[A], cbf: CanBuildFrom[Repr, A, To]): To = {
val b = cbf(it)
val iter = it.iterator
if (iter.hasNext) {
var pred = iter.next()
while (iter.hasNext) {
import num.mkNumericOps
val succ = iter.next()
b += succ - pred
pred = succ
}
}
b.result()
}
}
这会编译,但不会启动:
val stabs = IndexedSeq(1.0, 2.0, 3.0)
stabs.pairDiff
给出:value pairDiff is not a member of IndexedSeq[Double]
显式转换工作:
new RichIterableLike[Double, IndexedSeq[Double]](stabs).pairDiff
...如何解决这个问题?
编辑
如果我应用这个答案的方法,它会起作用:
implicit final class RichIterableLike[A, CC[~] <: Iterable[~]](val it: CC[A])
extends AnyVal {
def pairDiff[To](implicit num: Numeric[A], cbf: CanBuildFrom[CC[A], A, To]): To = {
...
}
但是问题仍然存在,在后一种情况下使隐式查找发挥作用的关键区别是什么。