2008-09-08 14 views
17

Sto cercando di mixin la MultiMap tratto con un HashMap in questo modo:MultiMap a Scala

val children:MultiMap[Integer, TreeNode] = 
    new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode] 

La definizione per il MultiMap tratto è:

trait MultiMap[A, B] extends Map[A, Set[B]] 

senso che un MultiMap di tipi A & B è un Map di tipi A & Set[B], o 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 inciampando di nuovo.

risposta

26

Ho dovuto importare scala.collection.mutable.Set. Sembra che il compilatore ritenesse che il Set in HashMap[Integer, Set[TreeNode]] fosse scala.collection.Set. Il Set nel MultiMap def è scala.collection.mutable.Set.

12

Questo può essere fastidioso, il sovraccarico del nome nelle collezioni di Scala è uno dei suoi punti deboli.

Per quello che vale, se si ha scala.collection._ importati, si potrebbe forse hanno scritto il vostro tipo HashMap come:

new HashMap[ Integer, mutable.Set[ TreeNode ] ] 
+1

In realtà è scala.collection._ :-) – fikovnik

+0

Hai ragione! Risolto, molte grazie. – Calum