Scala type of problems: SoftReference, ReferenceQueues, SoftHashMap

EDIT . Most of the answers so far have focused on the fact that I expanded the map incorrectly. I corrected this in the sample code, but type problems persist, and the question still remains.

I am trying to implement SoftHashMapin Scala, but am in a type mismatch error:

inferred type arguments [K,V] do not conform to class SoftValue type parameter bounds [K,+V <: AnyRef]
val sv = new SoftValue(kv._1, kv._2, queue)

I understand that I'm restricting types too much, but I'm not sure how to fix this.

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
  }
}
+3
source share
3 answers

It compiles. (edit: I initially added val value, but created a strong link). Not sure how you want to handle the iterator ...

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 <: AnyRef] extends Map[K, V] {
  private val map = new HashMap[K, SoftValue[K, V]]
  private val queue = new ReferenceQueue[V]

  def +=(kv: (K, V)): this.type = {
    val sv = new SoftValue(kv._1, kv._2, queue)
    map(kv._1) = sv
    this
  }

  def -=(k: K): this.type = { map -= k; this }
  def get(k: K) = map.get(k).flatMap(_.get)
  def iterator: Iterator[(K,V)] = error("todo")
}

V <: AnyRef SoftMap, V SoftValue.

+3

, , , scala.collection.immutable.Map, scala.Map.

-, V . , a SoftMap[String, Dog] SoftMap[String, Any]. .

trait Contra[+A] {
    def a: A    // method return type is a covariant position, okay
    def m(a: A) // method parameter is a contravariant position, error
    def n[AA >: A](a: AA) // upper bound of a type param is in covariant position, okay
}

Typesafe mutable collections - : . .

collection.mutable.Map :

trait Map[A, B] 
  extends Iterable[(A, B)]
     with scala.collection.Map[A, B] 
     with MapLike[A, B, Map[A, B]]

, :

scala> trait SoftMap[K, +V] extends collection.mutable.Map[K, V]
<console>:25: error: covariant type V occurs in invariant position in type [K,+V]java.lang.Object with scala.collection.mutable.Map[K,V] of trait SoftMap
       trait SoftMap[K, +V] extends collection.mutable.Map[K, V]
             ^

:

scala> trait SoftMap[K, V] extends collection.mutable.Map[K, V]
defined trait SoftMap

, , , + , , Scala 2.8. +=.

+1

SoftMap , V1 V. , - V1, - V. .

+ , +. , +=, . += - , .

, .

EDIT:

The error message that appeared after changing the question above is due to the fact that the Vsoft card type parameter can be of any type, and the type V SoftValuemust be a subtype AnyRef(cannot be Intor Float, that is, a subtype AnyVal). Therefore, you cannot create SoftValuewith a type Vfrom SoftMap, because someone can create an instance SoftMapby installing V, say Int.

+1
source

Source: https://habr.com/ru/post/1794872/


All Articles