斯卡拉Lift:解析 XML 时存在不明确的隐式转换

发布于 2024-10-26 03:28:39 字数 870 浏览 3 评论 0原文

我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-11-02 03:28:39

试试这个:

val key: String = x \ "@key" text
val value: String = x \ "@value" text

Try this:

val key: String = x \ "@key" text
val value: String = x \ "@value" text
孤千羽 2024-11-02 03:28:39

如果可能的话,将您的 JsonDSL 导入(或相反的 XML 导入)移至较小的范围。

class A {
  def doXmlStuff = { ... }
  def doJsonStuff = {
    import net.liftweb.json.JsonDSL._
    ...
  }
}

Move your JsonDSL-import (or reversely the XML-imports) into a smaller scope if possible.

class A {
  def doXmlStuff = { ... }
  def doJsonStuff = {
    import net.liftweb.json.JsonDSL._
    ...
  }
}
挽容 2024-11-02 03:28:39

通常解决此类问题的方法是缩小导入范围。在这种情况下,也许您不需要将 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 encloses appendActionsFromXml. Hard to say if that would work without seeing more context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文