Aladdin - Scala Bugtracking
[#1048] project: compiler priority: low category: bug
submitter assigned to status date submitted
Burak Burak open 2007-04-17 12:49:24.0
subject xml patterns and whitespace
code
// Lukas says: can you tell me why the third pattern below doesn't match?

import scala.xml._

object PatternMatch2 extends Application {
  val book: Node = <telbook>
      <entry>
       <name>Luc</name>
        <number>
         <where>mobile</where>
         <num>+41 79 421 30 09</num>
       </number>
        <number>
         <where>home</where>
         <num>+41 21 311 20 04</num>
       </number>
      </entry>
      <entry>
        <name>Bob</name>
        <number>
         <where>home</where>
         <num>+41 31 837 28 28</num>
       </number>
      </entry>
    </telbook>

  (book \ "entry") match {
    case Seq(<entry>{ _* }</entry>, <entry>{ _* }</entry>) =>
Console.println("ok")
  }

  (((book \ "entry")(0)) \ "_") match {
    case Seq(<name>{ _* }</name>, <number>{ _* }</number>, <number>
{ _* }</number>) => Console.println("ok2")
  }

  ((book \ "entry")(0)) match {
    case <entry><name>{ _* }</name><number>{ _* }</number><number>
{ _* }</number></entry> => Console.println("ok3")
  }
}
what happened
The mail introduced a whitespace that probably wasn't there -- as is, also the second pattern won't match, becau\
se the compiler stupidly translates it to presence of a Text(\n) node. that should be fixed.

Lukas' third pattern doesn't match because there is whitespace in the input value. It could be fixed by matching\ on Utility.trim(node) instead of node. Maybe the unapplySeq of Elem should do the trimming itself to avoid this\ sort of stuff.
what expected
[back to overview]
Changes of this bug report
Martin  edited on  2007-04-17 16:15:21.0