EDIT Most answers thus far are focusing on the fact that I was incorrectly extending Map. I have corrected this in the example code, but the type woes persist, and the question still stands.
I'm trying to implement a SoftHashMap
in Scala, but am running into a type mismatch error:
inferred type arguments [K,V] do not conform to class SoftValue's type parameter bounds [K,+V <: AnyRef]
val sv = new SoftValue(kv._1, kv._2, queue)
I understand that I'm over-constraining the types, but I am not sure how to fix it.
import scala.collection.mutable.{Map, HashMap}
import scala.ref._
class SoftValue[K, +V <: AnyRef](val key:K, value:V, queue:ReferenceQueue[V]) extends SoftReference(value, queue)
class SoftMap[K, V] extends Map[K, V]
{
private val map = new HashMap[K, SoftValue[K, V]]
private val queue = new ReferenceQueue[V]
override def +=(kv: (K, V)):this.type =
{
val sv = new SoftValue(kv._1, kv._2, queue)
map(kv._1) = sv
this
}
}