This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Embedded code in XML

No replies
Cay Horstmann
Joined: 2009-09-04,
User offline. Last seen 42 years 45 weeks ago.
The "Programming in Scala" book by Odersky, Spoon and Venners has this to say about code inside XML:

An expression inside a brace escape does not have to evaluate to an XML
node. It can evaluate to any Scala value. In such a case, the result is converted
to a string and inserted as a text node.

That is false, as one can easily see:

 <li>{42}</li>.child(0).getClass // it's an Atom[Int], not a Text node

Nodes don't get wrapped into atoms:

<li>{<p/>}</li>.child(0).getClass // It's an Elem

I expected that strings get wrapped into Text nodes, but it turns out that's not so.

<li>{"Fred"}</li>.child(0).getClass // It's an Atom[String]

This may be a fine point, but I am wondering where this is specified. Chapter 10 of the language reference doesn't have a lot of detail.

Thanks,

Cay

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland