Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiMap in Scala

Tags:

generics

scala

I'm trying to mixin the MultiMap trait with a HashMap like so:

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

The definition for the MultiMap trait is:

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

Meaning that a MultiMap of types A & B is a Map of types A & Set[B], or so it seems to me. However, the compiler complains:

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  

It seems that generics are tripping me up again.

like image 452
sblundy Avatar asked Sep 08 '08 18:09

sblundy


1 Answers

I had to import scala.collection.mutable.Set. It seems the compiler thought the Set in HashMap[Integer, Set[TreeNode]] was scala.collection.Set. The Set in the MultiMap def is scala.collection.mutable.Set.

like image 167
sblundy Avatar answered Oct 07 '22 18:10

sblundy