« Return to Thread: Variable binding oddity

Re: Variable binding oddity

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View in Thread

It's using an extractor, not a case class

=> http://www.scala-lang.org/docu/files/api/scala/xml/Elem$object.html

unapplySeq on the Elem object is defined to accept a node, so you can write:

val node : Node = ...

node match {
case Elem(...) => ...
}




On Tue, Jul 7, 2009 at 1:12 PM, Carsten Saager <csaager@...> wrote:
Hi,

can someone explain this to me:

scala> import xml._
import xml._

scala> val a:Node = <a/>
a: scala.xml.Node = <a></a>

scala> a match {case e:Elem => e}
res0: scala.xml.Elem = <a></a>

scala> a match {case e @ Elem(_,_,_,_,_*) => e}
res1: scala.xml.Node = <a></a>

Why Node and not Elem?

This works as expected:

scala> class Foo(a:Int)
defined class Foo

scala> case class Bar(i:Int) extends Foo(-i)
defined class Bar

scala> val f:Foo=Bar(1)
f: Foo = Bar(1)

scala> f match {case b:Bar => b}
res6: Bar = Bar(1)

scala> f match {case b @ Bar(_) => b}
res7: Bar = Bar(1)

-Carsten

 « Return to Thread: Variable binding oddity