How to implement collection with covariance when delegating to another collection for storage?
- by memelet
I'm trying to implement a type of SortedMap with extended semantics. I'm trying to delegate to SortedMap as the storage but can't get around the variance constraints:
class IntervalMap[A, +B](implicit val ordering: Ordering[A])
//extends ...
{
var underlying = SortedMap.empty[A, List[B]]
}
Here is the error I get. I understand why I get the error (I understand variance). What I don't get is how to implement this type of delegation. And yes, the covariance on B is required.
error: covariant type B occurs in contravariant position in type scala.collection.immutable.SortedMap[A,List[B]] of parameter of setter underlying_=