未找到隐含值

时间:2010-09-15 08:20:49

标签: scala implicit

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 _ =>
    }
}

为什么编译器抱怨:

错误:找不到参数map的隐含值:scala.collection.mutable.Map [String,T] names.foreach(createEntity [T])

1 个答案:

答案 0 :(得分:4)

如果你打电话,例如,createEntities[Parent]("A", "B")(你可以,因为Parent是[{1}}的子类型),它需要一个隐含的Parent,并且没有'一个。更准确地说,您的定义要求您为 {em}的每个子类型提供mutable.Map[String, Parent],而不仅仅是那些已定义的

mutable.Map[String, T]