xiefei
xiefei

Reputation: 6609

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 the compiler complains:

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

?

Upvotes: 3

Views: 1681

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170859

If you call, e.g., createEntities[Parent]("A", "B") (which you can, because Parent is a subtype of Parent), it needs an implicit mutable.Map[String, Parent], and there isn't one. To be more precise, your definitions require you to supply a mutable.Map[String, T] for every subtype of Parent, not just those already defined:

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

Upvotes: 4

Related Questions