class Test {
import scala.collection._
class Parent
class Child extends Parent
implicit val children = mutable.Map[String, Child]()
def createEntities[T <: Parent](names: String*) = names.foreach(createEntity[T])
def createEntity[T <: Parent](name: String)(implicit map: mutable.Map[String, T]): Unit = map.get(name) match {
case None => println( name + " not defined.")
case _ =>
}
}
Why the compiler complains:
error: could not find implicit value for parameter map: scala.collection.mutable.Map[String,T]
names.foreach(createEntity[T])
?
If you call, e.g.,
createEntities[Parent]("A", "B")(which you can, becauseParentis a subtype ofParent), it needs an implicitmutable.Map[String, Parent], and there isn’t one. To be more precise, your definitions require you to supply amutable.Map[String, T]for every subtype ofParent, not just those already defined: