斯卡拉Lift:解析 XML 时存在不明确的隐式转换
我有一个从 xml-Node 中提取操作对象的方法:
private def appendActionsFromXml(device: Device, xml: Node) = {
xml \ "actions" \ "action" map {
x => {
val key = x \ "@key" text
val value = x \ "@value" text
device.createAction(key, value)
}
}
}
但是,由于我在同一个类中导入了 import net.liftweb.json.JsonDSL._ ,所以当我提取“@key”属性时,我会遇到歧义来自 x:
[INFO] Note that implicit conversions are not applicable because they are ambiguous
[INFO] both method string2jvalue in trait Implicits of type (x: String)net.liftweb.json.JsonAST.JString
[INFO] and method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
[INFO] are possible conversion functions from String to ?{val apply: ?}
[INFO] val value = x \ "@value" text
我如何解决这种特定方法中的这种歧义?
I have a method that extracts action-objects from an xml-Node:
private def appendActionsFromXml(device: Device, xml: Node) = {
xml \ "actions" \ "action" map {
x => {
val key = x \ "@key" text
val value = x \ "@value" text
device.createAction(key, value)
}
}
}
However, since I have imported import net.liftweb.json.JsonDSL._ in the same class, I get an ambigouity when I extract the "@key"-attribute from x:
[INFO] Note that implicit conversions are not applicable because they are ambiguous
[INFO] both method string2jvalue in trait Implicits of type (x: String)net.liftweb.json.JsonAST.JString
[INFO] and method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
[INFO] are possible conversion functions from String to ?{val apply: ?}
[INFO] val value = x \ "@value" text
How do I resolve this ambigouity in this perticular method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
如果可能的话,将您的 JsonDSL 导入(或相反的 XML 导入)移至较小的范围。
Move your JsonDSL-import (or reversely the XML-imports) into a smaller scope if possible.
通常解决此类问题的方法是缩小导入范围。在这种情况下,也许您不需要将
net.liftweb.json.JsonDSL._
包含在包含appendActionsFromXml
的范围内。如果没有更多的背景信息,很难说这是否有效。Usually the way to resolve this kind of problem is to reduce the scope of the import. In this case perhaps you don't need to have
net.liftweb.json.JsonDSL._
in scope within the scope that enclosesappendActionsFromXml
. Hard to say if that would work without seeing more context.