I have the following code being compiled against scala 2.8.0:
import scala.util.parsing.combinator.{syntactical,PackratParsers}
import syntactical.StandardTokenParsers
object MyParser extends StandardTokenParsers with PackratParsers{
lexical.reserved ++= Set("int","char","boolean")
lazy val primitiveType:PackratParser[PrimitiveType[_]] = primitiveChar | primitiveInt | primitiveBool
lazy val primitiveInt:PackratParser[PrimitiveType[Int]] = "int" ^^ { _ => PrimitiveType[Int]() }
lazy val primitiveChar:PackratParser[PrimitiveType[Char]] = "char" ^^ { _ => PrimitiveType[Char]() }
lazy val primitiveBool:PackratParser[PrimitiveType[Boolean]] = "boolean" ^^ { _ => PrimitiveType[Boolean]() }
}
object MyParser2 extends StandardTokenParsers with PackratParsers{
lexical.reserved ++= Set("int","char","boolean")
lazy val primitiveType:PackratParser[PrimitiveType[_]] = primitiveChar | primitiveIntOrBool
lazy val primitiveIntOrBool:PackratParser[PrimitiveType[_]] = "int" ^^ { _ => PrimitiveType[Int]() } | "boolean" ^^ {_ => PrimitiveType[Boolean]()}
lazy val primitiveChar:PackratParser[PrimitiveType[Char]] = "char" ^^ { _ => PrimitiveType[Char]()}
}
case class PrimitiveType[T]()
Compiling MyParser1 gives:
error: inferred type arguments [this.PrimitiveType[_ >: _1 with Boolean <: AnyVal]] do not conform to method |'s type parameter bounds [U >: this.PrimitiveType[_ >: Char with Int <: AnyVal]]
I believe it fails because of the | method type signature, defined as:
def | [U >: T](q: => Parser[U]): Parser[U]
why does U have to be a supertype of T? What should be the return value of "primitiveType"?