- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
A question about type conversion in a parser
Sat, 2009-12-19, 16:40
Hi gurus -
I have some code like this -
package parserquestion
import scala.util.parsing.combinator._
class HelloWorldParser extends JavaTokenParsers{
def helloWorld="hello: "~ (name | dogName) ^^ {
case hello~na=>
na match{
case n:Name=>println(n)
case n:DogName=>println("dog, "+n)
}
}
def name=first ~ last^^{
case f ~l=>new Name(f,l)
}
def first=ident
def last=ident
def dogName=first^^{
case f=>new DogName(f)
}
}
case class Name(val first:String,val last:String)
case class DogName(val name:String)
When compiling with "-Xprint:Typer" option, I got this -
[scalac] def helloWorld: HelloWorldParser.this.Parser[Unit] = HelloWorldParser.this.literal("hello: ").~[Product](HelloWorldParser.this.name.|[Product](HelloWorldParser.this.dogName)).^^[Unit](((x0$3: HelloWorldParser.this.~[String,Product]) => x0$3 match {
[scalac] case (_1: String,_2: Product)HelloWorldParser.this.~[String,Product]((hello @ _), (na @ _)) => na match {
[scalac] case (n @ (_: parserquestion.Name)) => scala.this.Predef.println(n)
[scalac] case (n @ (_: parserquestion.DogName)) => scala.this.Predef.println("dog, ".+(n))
[scalac] }
[scalac] }));
So, the question is -
How the na in instance "~[String,Product]" match _:Name or _:DogName, does this mean Product[T] match case t:T?
I have some code like this -
package parserquestion
import scala.util.parsing.combinator._
class HelloWorldParser extends JavaTokenParsers{
def helloWorld="hello: "~ (name | dogName) ^^ {
case hello~na=>
na match{
case n:Name=>println(n)
case n:DogName=>println("dog, "+n)
}
}
def name=first ~ last^^{
case f ~l=>new Name(f,l)
}
def first=ident
def last=ident
def dogName=first^^{
case f=>new DogName(f)
}
}
case class Name(val first:String,val last:String)
case class DogName(val name:String)
When compiling with "-Xprint:Typer" option, I got this -
[scalac] def helloWorld: HelloWorldParser.this.Parser[Unit] = HelloWorldParser.this.literal("hello: ").~[Product](HelloWorldParser.this.name.|[Product](HelloWorldParser.this.dogName)).^^[Unit](((x0$3: HelloWorldParser.this.~[String,Product]) => x0$3 match {
[scalac] case (_1: String,_2: Product)HelloWorldParser.this.~[String,Product]((hello @ _), (na @ _)) => na match {
[scalac] case (n @ (_: parserquestion.Name)) => scala.this.Predef.println(n)
[scalac] case (n @ (_: parserquestion.DogName)) => scala.this.Predef.println("dog, ".+(n))
[scalac] }
[scalac] }));
So, the question is -
How the na in instance "~[String,Product]" match _:Name or _:DogName, does this mean Product[T] match case t:T?