I have a problem using a parameterized class as the key-type of a Map
. First create the parameterized class:
scala> sealed abstract class Foo[T]{ def t: T }
defined class Foo
Now create some imaginary collection of these across unknown parameters:
scala> var l: List[Foo[_]] = Nil
l: List[Foo[_]] = List()
Now create a Map to store these in:
scala> var mm: Map[Foo[_], Any] = Map.empty
mm: Map[Foo[_],Any] = Map()
Now attempt to populate the Map
scala> l.foreach { foo: Foo[_] =>
| mm += (foo -> "BAR")
| }
Gives me the following compiler error:
<console>:9: error: type mismatch; found : foo.type (with underlying type Foo[_]) required: Foo[_$1] where type _$1 mm += (foo -> "BAR") ^
This compiles just fine in 2.8. Is there any workaround to get this to work in 2.7?
EDIT - this also worked for me:
var mm: Map[AnyRef, Any] = Map.empty //note not Foo[_]
scala> l.foreach { foo: Foo[_] =>
| mm += ( (foo: AnyRef) -> "BAR") //still have to tell compiler foo is an AnyRef
| }