How to add a XML node for Some case in pattern matching?



I want that the nodes test1, test2 and test3 appear only if option is defined. The code below works, however, I don't like test1 because it does not use pattern matching (and so I need to call option.get); I don't like test2 because I have a useless line case _ =>. and I don't like test3 because I have the equivalent .getOrElse(()). Is there a nice way to achieve this?



val option: Option[Int] = None

val node =
<test>
{ if (option.isDefined) <test1>{option.get}</test1> }

{ option match {
case Some(x) => <test2>{x}</test2>
case _ =>
}}

{ option.map(x => <test3>{x}</test3>).getOrElse(()) }
</test>

No comments:

Post a Comment