I am trying to define a map with a parameterized class as its key. But when I try to add to it I get a compiler error:
trait MyTrait[T <: MyTrait[T]]
case class A(i: Int) extends MyTrait[A]
case class B(str: String) extends MyTrait[B]
var map = Map[Class[_ <: MyTrait[_]], Int]()
def update[T <: MyTrait[T]](n: Int) = {
map += classOf[T] -> n // Won't compile
}
I get the following compiler error:
Expression does not convert to assignment because:
class type required but T found
expansion: map = map.+(classOf[T].<$minus$greater: error>(n))
map += classOf[T] -> n
What is the correct way to extract my class as a key? It compiles fine if I use a concrete class for example:
map += classOf[A] -> n