如何根据选项添加或不添加 XML 属性?

发布于 2024-10-14 01:11:25 字数 431 浏览 0 评论 0原文

我写了一个 makeMsg 函数,但我不喜欢它 - 根据 Option.isDefined 进行区分似乎真的不符合 Scala 风格。你能做得更好吗?

scala> def makeMsg(t: Option[String]) = 
     | if (t.isDefined) <msg text={t.get} /> else <msg />
makeMsg: (t: Option[String])scala.xml.Elem

scala> makeMsg(Some("hello"))
res0: scala.xml.Elem = <msg text="hello"></msg>

scala> makeMsg(None)
res1: scala.xml.Elem = <msg></msg>

I have written a makeMsg function but I don't like it - it just seems really un-Scala-ish to discriminate based on Option.isDefined. Can you make it better?

scala> def makeMsg(t: Option[String]) = 
     | if (t.isDefined) <msg text={t.get} /> else <msg />
makeMsg: (t: Option[String])scala.xml.Elem

scala> makeMsg(Some("hello"))
res0: scala.xml.Elem = <msg text="hello"></msg>

scala> makeMsg(None)
res1: scala.xml.Elem = <msg></msg>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

早乙女 2024-10-21 01:11:25

您可以尝试以下操作:

def makeMsg(t: Option[String]) = <msg text={t orNull} />

如果属性值为 null - 它将不会添加到元素中。

更新

更好!如果您要添加此隐式转换:

import xml.Text
implicit def optStrToOptText(opt: Option[String]) = opt map Text

您可以像这样使用 t

def makeMsg(t: Option[String]) = <msg text={t} />

这是 REPL 会话:

scala> import xml.Text
import xml.Text

scala> implicit def optStrToOptText(opt: Option[String]) = opt map Text
optStrToOptText: (opt: Option[String])Option[scala.xml.Text]

scala> def makeMsg(t: Option[String]) = <msg text={t} />
makeMsg: (t: Option[String])scala.xml.Elem

scala> makeMsg(Some("hello"))
res1: scala.xml.Elem = <msg text="hello"></msg>

scala> makeMsg(None)
res2: scala.xml.Elem = <msg ></msg>

这有效,因为 scala.xml.UnprefixedAttribute 具有接受 的构造函数Option[Seq[Node]] 作为值。

You can try this:

def makeMsg(t: Option[String]) = <msg text={t orNull} />

if attribute value is null - it will not be added to the element.

Update

Even better! If you will add this implicit convertion:

import xml.Text
implicit def optStrToOptText(opt: Option[String]) = opt map Text

you can just use t like this:

def makeMsg(t: Option[String]) = <msg text={t} />

Here is REPL session:

scala> import xml.Text
import xml.Text

scala> implicit def optStrToOptText(opt: Option[String]) = opt map Text
optStrToOptText: (opt: Option[String])Option[scala.xml.Text]

scala> def makeMsg(t: Option[String]) = <msg text={t} />
makeMsg: (t: Option[String])scala.xml.Elem

scala> makeMsg(Some("hello"))
res1: scala.xml.Elem = <msg text="hello"></msg>

scala> makeMsg(None)
res2: scala.xml.Elem = <msg ></msg>

This works because scala.xml.UnprefixedAttribute has constructor that accepts Option[Seq[Node]] as value.

伴随着你 2024-10-21 01:11:25

这有什么问题:

def makeMsg(t: Option[String]) = t match {
  case Some(m) => <msg text={m} />
  case None => <msg />
}

不像Easy Angel,但它是直接的 Scala。

What's wrong with this:

def makeMsg(t: Option[String]) = t match {
  case Some(m) => <msg text={m} />
  case None => <msg />
}

Not as concise as Easy Angel's, but it's straight up Scala.

生寂 2024-10-21 01:11:25

Canonical Scala 不要求文本字段知道在空时巧妙地消失:

t.map(s => <msg text={s} />).getOrElse(<msg />)

每当您有选项但需要使用不知道选项的内容时,您应该考虑使用此模式。 (在这种情况下,Easy Angel 找到了一个更紧凑的解决方案,它确实了解选项或类似的内容。)

Canonical Scala that does not require the text field to know to cleverly disappear when it's empty:

t.map(s => <msg text={s} />).getOrElse(<msg />)

You should think about using this pattern whenever you have an option but need to use something that doesn't know about options. (In this case, Easy Angel has found a more compact solution where it does know about options or something like them.)

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