Please note this question is strictly related to returning 0 or 1 elements (Node or Elem). Please don't consider NodeSeq.Empty as a solution, as that already works for functions returning NodeSeq type (0 or more elements), as solved on this question.
I'm building an XML by pieces using different functions such as the following example:
<xml>
{ maybeXmlNode(param) }
</xml>
And trying to return an Empty or Non-empty Node (or Elem) based on the value of the param such as:
def maybeXmlNode(param: Boolean): NodeOrElem = {
if(param) <someXml></someXml>
else ??? //Empty or None doesn't work
}
The solution I'm using now is just defining the function type as Option[Elem] and then using it as maybeXml.getOrElse(""), but that doesn't make that much sense to me. My current usage is as follows:
<xml>
{ maybeXmlNode(param).getOrElse("") }
</xml>
def maybeXmlNode(param: Boolean): Option[Elem] = {
if(param) Some(<someXml></someXml>)
else None
}
Does a better way to express this exists, probably by using an Empty Node or Elem directly?