I have the following extension class that adds a myAppend
method to anything 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
}
How can I port this code to Scala 2.13 and retain similar performance characteristics? Bonus points if the extended class can remain an AnyVal
Few things I have tried:
class Extends1[R, S <: IsSeq[R]](c: R, isSeq: S) {
def myAppend(a: isSeq.A): R = (isSeq(c) :+ a).asInstanceOf[R]
}
But the asInstanceOf
is disappointing - is it even safe?
I could do:
class Extends3[S[_], A](c: SeqOps[A, S, S[A]]) {
def myAppend(a: A): S[A] = c :+ a
}
but now we're constrained to collections of the form S[A]
while the Scala 2.12 code can take any R
.