Skip to content

Instantly share code, notes, and snippets.

@bartschuller
Created May 5, 2012 21:09
Show Gist options
  • Save bartschuller/2605607 to your computer and use it in GitHub Desktop.
Save bartschuller/2605607 to your computer and use it in GitHub Desktop.
Scala 2.10 Anti-XML literals with interpolation demo
// some data to interpolate
val attributes = Attributes("a"->"data wins", "b" -> "data loses")
val attributeValue = "one"
val nodes = Group(Elem("hi", Attributes()), Text(" there"))
// Scala 2.10's pluggable string interpolation
// Here xml"" means: create an Anti-XML element
val xml = xml"""<foo a="overridden" $attributes b="literal wins">blie <b attr=$attributeValue/> bla $nodes</foo>"""
// note the 3 different kinds of interpolation
// - a set of attributes, with duplicate handling any way you want
// - a single attribute value
// - any Anti-XML Node, Group or anything else converted to a String
println(xml)
// prints
<foo a="data wins" b="literal wins">blie <b attr="one"/> bla <hi/> there</foo>
// See https://github.com/bartschuller/210-experiments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment