This page is no longer maintained — Please continue to the home page at

A question about type conversion in a parser

No replies
linjie nie
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.
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](|[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?

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