我有以下扩展类,它myAppend
向任何东西添加了一个方法SeqLike
。
implicit class WithAppend[A, R](s: SeqLike[A, R]) extends AnyVal {
def myAppend(i: A)(implicit cbf: CanBuildFrom[R, A, R]): R = s :+ i
}
如何将此代码移植到 Scala 2.13 并保留类似的性能特征?如果扩展课程可以保持AnyVal
我尝试过的几件事:
class Extends1[R, S <: IsSeq[R]](c: R, isSeq: S) {
def myAppend(a: isSeq.A): R = (isSeq(c) :+ a).asInstanceOf[R]
}
但asInstanceOf
令人失望的是——它甚至安全吗?
我可以:
class Extends3[S[_], A](c: SeqOps[A, S, S[A]]) {
def myAppend(a: A): S[A] = c :+ a
}
但是现在我们受限于表单的集合,S[A]
而 Scala 2.12 代码可以采用任何R
.