Implicit value not found

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 does the compiler complain:

error: could not find an implicit value for the parameter map: scala.collection.mutable.Map [String, T] names.foreach (CreateEntity [T])

?

+3
source share
1 answer

If you call, for example, createEntities[Parent]("A", "B")(which you can, because it Parentis a subtype Parent), it needs an implicit one mutable.Map[String, Parent], and it is not. To be more precise, your definitions require you to specify mutable.Map[String, T]for each subtype Parent, and not just for those already defined:

implicit def aMap[T <: Parent]: mutable.Map[String, T] = ...
+4
source

Source: https://habr.com/ru/post/1764840/


All Articles