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

XML matching oddity

2 replies
mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.
I was working on  XHTML conversion routine and ended with a behavior I'd like explained.

On the site I collect data from, sometime people do <B><IMG src="x.gif"/></B>.

I need the image but not the bold around it, among other thing I do some processing:


val node = <B>Alpha</B>


node match {
      case <BR></BR> => println("break")

      case bi @ <B><IMG/></B>  => println("Bold image")  //THIS IS THE BAD GUY
      case <B>{text @ _*}</B> => println("Bold blob "+text)
      case <B>{text}</B> => print("Bold coin "+text) //Need because of BAD GUY

}

Now after I added the line (named BAD GUY)  I need an extra <B>{text}</B> or I get a match error. Can some one explain why this happens?

Thomas
Cay Horstmann
Joined: 2009-09-04,
User offline. Last seen 42 years 45 weeks ago.
XML matching oddity
I am pretty sure this is a bug. When I try this in the REPL node match {      case
=> "break"      case bi @  => "Bold image " + bi      case {text @ _*} => "Bold other " + text.toString()      case _ => "something else" } I kill it with a stack trace  Exception in thread "main" scala.tools.nsc.symtab.Types$TypeError: too many arguments for method body%2: ()java.lang.String        at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:318)        at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:268)        at scala.tools.nsc.typechecker.Infer$Inferencer.errorTree(Infer.scala:272)        ,,, But the following works fine: node match {      case
=> "break"      case bi @  => "Bold image " + bi      case {text @ _*} => "Bold other "      case _ => "something else" } The only difference is that I am not using the text variable. Cheers, Cay On Thu, Dec 3, 2009 at 6:40 PM, Thomas Sant Ana wrote: > I was working on  XHTML conversion routine and ended with a behavior I'd > like explained. > > On the site I collect data from, sometime people do src="http://www.scala-lang.org/x.gif"/>. > > I need the image but not the bold around it, among other thing I do some > processing: > > > val node = Alpha > > > node match { >       case
=> println("break") > >       case bi @   => println("Bold image")  //THIS IS THE BAD > GUY >       case {text @ _*} => println("Bold blob "+text) >       case {text} => print("Bold coin "+text) //Need because of BAD > GUY > > } > > Now after I added the line (named BAD GUY)  I need an extra {text} or > I get a match error. Can some one explain why this happens? > > Thomas >
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: XML matching oddity
If you get an exception when you compile something, it is a bug.

On Sat, Dec 5, 2009 at 3:05 AM, Cay Horstmann <cay.horstmann@gmail.com> wrote:
I am pretty sure this is a bug. When I try this in the REPL

node match {
     case <BR></BR> => "break"
     case bi @ <B><IMG/></B>  => "Bold image " + bi
     case <B>{text @ _*}</B> => "Bold other " + text.toString()
     case _ => "something else"
}

I kill it with a stack trace

 Exception in thread "main" scala.tools.nsc.symtab.Types$TypeError:
too many arguments for method body%2: ()java.lang.String
       at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:318)
       at scala.tools.nsc.typechecker.Infer$Inferencer.error(Infer.scala:268)
       at scala.tools.nsc.typechecker.Infer$Inferencer.errorTree(Infer.scala:272)
       ,,,

But the following works fine:
node match {
     case <BR></BR> => "break"
     case bi @ <B><IMG/></B>  => "Bold image " + bi
     case <B>{text @ _*}</B> => "Bold other "
     case _ => "something else"
}

The only difference is that I am not using the text variable.

Cheers,

Cay

On Thu, Dec 3, 2009 at 6:40 PM, Thomas Sant Ana <mailleux@gmail.com> wrote:
> I was working on  XHTML conversion routine and ended with a behavior I'd
> like explained.
>
> On the site I collect data from, sometime people do <B><IMG
> src="x.gif"/></B>.
>
> I need the image but not the bold around it, among other thing I do some
> processing:
>
>
> val node = <B>Alpha</B>
>
>
> node match {
>       case <BR></BR> => println("break")
>
>       case bi @ <B><IMG/></B>  => println("Bold image")  //THIS IS THE BAD
> GUY
>       case <B>{text @ _*}</B> => println("Bold blob "+text)
>       case <B>{text}</B> => print("Bold coin "+text) //Need because of BAD
> GUY
>
> }
>
> Now after I added the line (named BAD GUY)  I need an extra <B>{text}</B> or
> I get a match error. Can some one explain why this happens?
>
> Thomas
>



--
Daniel C. Sobral

I travel to the future all the time.

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