Domanda
Sto cercando di mixare i MultiMap
tratto con a HashMap
così:
val children:MultiMap[Integer, TreeNode] =
new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode]
La definizione per il MultiMap
caratteristica è:
trait MultiMap[A, B] extends Map[A, Set[B]]
Ciò significa che a MultiMap
di tipi A
& B
è un Map
di tipi A
& Set[B]
, O almeno così mi sembra.Tuttavia, il compilatore si lamenta:
C:\...\TestTreeDataModel.scala:87: error: illegal inheritance; template $anon inherits different type instances of trait Map: scala.collection.mutable.Map[Integer,scala.collection.mutable.Set[package.TreeNode]] and scala.collection.mutable.Map[Integer,Set[package.TreeNode]]
new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode]
^ one error found
Sembra che i generici mi stiano di nuovo facendo lo sgambetto.
Soluzione
Ho dovuto importare scala.collection.mutable.Set
.Sembra che il compilatore abbia pensato al Set in HashMap[Integer, Set[TreeNode]]
era scala.collection.Set
.Il set nella MultiMap è def scala.collection.
mutable
.Set
.
Altri suggerimenti
Ciò può essere fastidioso, il sovraccarico di nomi nelle collezioni di Scala è uno dei suoi grandi punti deboli.
Per quello che vale, se lo avessi scala.collection._
importato, probabilmente avresti potuto scrivere il tuo HashMap
digitare come:
new HashMap[ Integer, mutable.Set[ TreeNode ] ]