пʼятницю, 30 листопада 2007 р.

scala puzzle

Now playing with scala )

First disappointment: type system is not such intuitive as expected:


var x = qq
var y = new Elem(x.prefix,x.label,x.attributes,x.scope,x.child)


will raise error:

found : scala.xml.Node*
required: scala.xml.Node


for x.child. Why ?

Update: I got answer in scala mail list from Eric Torreborre :

The error message may be misleading but Elem is expecting Node elements like
this:

var y = Elem(x.prefix,x.label,x.attributes,x.scope,x.child.last, x.child.last) //to add 2 children nodes


So if you want to pass all the children from x, you have to write:

var y = Elem(x.prefix,x.label,x.attributes,x.scope,x.child: _*)


You can also notice that "new" is unecessary as there is a factory method in
the companion object of class Elem:
...

Great. Scala mail list support is really good. But .. yet - why x.child is a Seq[Node] but Elem constructor understand one as Node ?
- because Elem constructor try to associate child (type Seq[Node]) with first element of Node* (type Node). I.e. difference between Node* and Seq[Node] that Node* must be represented as few functional arguments (or as special _* adapter).