This page is no longer maintained — Please continue to the home page at

Node constructor type

No replies
Jon Pretty
Joined: 2009-02-02,
User offline. Last seen 42 years 45 weeks ago.

Hi everyone,

Currently, we can stick any old type into an XML block, e.g.:



myObject.toString"Did myObject.toString")

But, oh dear!, I've been very naive and my node contents now evaluates
to Unit. But the typechecker hasn't warned me, and I'm now confused as
to why my node looks empty. I'm probably about to start looking into
the toString method on myObject.

So can we do anything about this? XML blocks take Anys, and determine
the content type by reflection. Whilst this unfortunately permits the
above, it also allows you to write:

if(condition) "string" else

which overloading the node constructor wouldn't.

But could we change the constructor to take Seq[Node] and define implicits:

import scala.xml._
def nodeToSeqNode(n : Node) = List(n)
def stringToSeqNode(s : String) = List(Text(s))
def intToSeqNode(i : Int) = List(Text(i.toString))

in Predef?

Does this work?


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