Writing an XML literal as a parameter in Scala

I can pass the variable as a multi-valued parameter:

scala> <b/> res26: scala.xml.Elem = <b></b> scala> Elem(null,"a",Null,TopScope,res26) res27: scala.xml.Elem = <a><b></b></a> 

But I can not pass the XML literal as a multivalued parameter:

 scala> Elem(null,"a",Null,TopScope,<b/>) <console>:12: error: not found: value < Elem(null,"a",Null,TopScope,<b/>) 

But I can pass the XML literal as a simple parameter

 scala> def bar(s:String,n:Elem) = s+n.toString bar: (s: String, n: scala.xml.Elem)java.lang.String scala> bar("super ", <a/>) res30: java.lang.String = super <a></a> 

?

thanks

+6
source share
1 answer

Adding a space before an XML element makes it work:

 scala> Elem(null, "a", Null, TopScope, <b/>) resN: scala.xml.Elem = <a><b></b></a> 

From Scala Language Specification , Section 1.5:

To allow literal inclusion of XML fragments, lexical analysis switches from Scala in XML mode when an opening angle bracket <occurs in the following circumstance: it must be preceded either by a space or by opening a bracket or an opening bracket and immediately followed by a character starting with the XML name

+8
source

Source: https://habr.com/ru/post/898050/


All Articles