Please note that Map
extends MapFactory.Delegate[Map](HashMap)
. You can see it Scala source code:
object Map extends MapFactory.Delegate[Map](HashMap) {
In MapFactory.Delegate
there is an apply
method, which is the one you call (the first method in this class):
class Delegate[C[_, _]](delegate: MapFactory[C]) extends MapFactory[C] {
override def apply[K, V](elems: (K, V)*): C[K, V] = delegate.apply(elems: _*)
def from[K, V](it: IterableOnce[(K, V)]): C[K, V] = delegate.from(it)
def empty[K, V]: C[K, V] = delegate.empty
def newBuilder[K, V]: Builder[(K, V), C[K, V]] = delegate.newBuilder
}
From your comments, it seems like you want to implement a custom update. You can try this function:
def customUpdate[K](m: mutable.Map[K, Int], key: K, value: Option[Int]): Unit = {
value.fold {
m.remove(key)
} { newValue =>
m.updateWith(key) {
case None =>
Some(newValue)
case Some(currentValue) =>
Some(currentValue + newValue)
}
}
}
which created the expected output as in your comments. Code run in Scastie.
scala.collection.mutable.Map[Int, Int]()
is not calling a constructor is calling theapply
method on themutable.Map
companion object... why do you need / want to extend theMap
class? - Luis Miguel Mejía Suárezapply
? or can you live with just an extension method? And do you need it to be mutable? - Also, BTW, your customapply
looks the Monoid ofMap
. - Luis Miguel Mejía Suárez