未找到隐式值
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[String,T] 名称.foreach(createEntity[T])
?
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])
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您调用,例如
createEntities[Parent]("A", "B")
(您可以这样做,因为Parent
是Parent
的子类型code>),它需要一个隐式的mutable.Map[String, Parent]
,但目前还没有。更准确地说,您的定义要求您为Parent
的每个子类型提供mutable.Map[String, T]
,而不仅仅是那些已经定义:If you call, e.g.,
createEntities[Parent]("A", "B")
(which you can, becauseParent
is 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: