I'm building an XML by pieces using different functions such as the following example:
<xml>
{ maybeXml(param) }
</xml>
And trying to return an Empty or Non-empty NodeSeq based on the value of the param such as:
def maybeXml(param: Boolean): NodeSeq = {
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[NodeSeq] and then using it as maybeXml.getOrElse(""), but that doesn't make that much sense to me. My current usage is as follows:
<xml>
{ maybeXml(param).getOrElse("") }
</xml>
def maybeXml(param: Boolean): NodeSeq = {
if(param) Some(<someXml></someXml>)
else None
}
Is a better way to express this using an Empty NodeSeq directly?
For empty NodeSeq
use NodeSeq.Empty
def maybeXml(param: Boolean): NodeSeq = {
if(param) <someXml></someXml>
else NodeSeq.Empty
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With