3
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 _ =>
}
}
爲什麼編譯器會抱怨:隱含的價值沒有發現
錯誤:無法找到參數映射隱含值:scala.collection.mutable.Map [字符串,T] names.foreach(createEntity [T ])
?
這是'names.foreach(createEntity [T])'調用,它需要一個隱含的論點。如果你刪除這個調用,當然它編譯! – 2010-09-15 10:32:00
明白了。調用:createEntity [Child]()但不createEntity [T]()是合法的,因爲對於mutable.Map [String,Child]有一個隱式值,但對於mutable.Map [String,T]沒有隱式值 – xiefei 2010-09-15 11:53:12
是的,精確的。 – 2010-09-15 12:03:31