I’m trying to extend a SortedMap in Scala, but I’m having some problems with SortedMapLike and canBuildFrom (the last one I can’t even type it correctly). Here’s some code; first the companion object:
object Timeline {
...
def newBuilder[A]: Builder[(Long, A), Timeline[A]] =
new ListBuffer[(Long, A)] mapResult fromSeq
def fromSeq[A](buf: Seq[(Long, A)]): Timeline[A] =
new Timeline(buf toMap)
def empty[A] = Timeline[A](Map[Long, A]())
}
Then the class (yes, all my Timelines are from Long to A):
final class Timeline[A] private(t: Map[Long, A])
extends SortedMap[Long, A]
with SortedMapLike[Long, A, Timeline[A]] {
private[this] lazy val iMap =
TreeMap(t.toArray: _*)(Ordering.fromLessThan[Long](_ > _))
override def newBuilder: Builder[(Long, A), Timeline[A]] = Timeline.newBuilder
override def empty: Timeline[A] = Timeline.empty
def -(key: Long) = Timeline(iMap - key)
def get(key: Long) = iMap.get(key)
def rangeImpl(from: Option[Long], until: Option[Long]) =
Timeline(iMap.rangeImpl(from, until))
def iterator = iMap.iterator
def ordering = iMap.ordering
}
I’m not sure all the above is the correct way to achieve this, but now comes the part I can’t even type correctly:
implicit def canBuildFrom[A]: CanBuildFrom[Timeline[A], A, Timeline[A]] =
new CanBuildFrom[Timeline[A], A, Timeline[A]] {
def apply(): Builder[(Long, A), Timeline[A]] = newBuilder[A]
def apply(from: Timeline[A]): Builder[(Long, A), Timeline[A]] = newBuilder[A]
}
It seems I was incorrectly typing
canBuildFrom: