我根据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]()
编译MyParser1给出:
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]]
我认为因为|而失败了方法类型签名,定义为:
def | [U >: T](q: => Parser[U]): Parser[U]
为什么U必须是T的超类型?什么应该是“primitiveType”的返回值?
答案 0 :(得分:5)
您需要将最后一行更改为
case class PrimitiveType[+T]()
这允许PrimitiveType [Int]&lt;:PrimitiveType [AnyVal],当你想通过|合并PrimitiveType [Boolean]和PrimitiveType [Int]解析器的结果时需要它。
顺便说一句,我也建议写PrimitiveType[AnyVal]
而不是
PrimitiveType[_]
因为在你的情况下这更精确。
答案 1 :(得分:3)
您可能最好将通用case class PrimitiveType[T]
更改为类层次结构。泛型类型参数在运行时不可用,因此您无法对解析结果做很多事情......
这将为您提供以下内容(未经测试的代码现已测试):
object MyParser extends StandardTokenParsers with PackratParsers{
lexical.reserved ++= Set("int","char","boolean")
lazy val primitiveType:Parser[PrimitiveType] = primitiveChar | primitiveInt | primitiveBool
lazy val primitiveInt:PackratParser[PrimitiveType] = "int" ^^^ PrimitiveInt
lazy val primitiveChar:PackratParser[PrimitiveType] = "char" ^^^ PrimitiveChar
lazy val primitiveBool:PackratParser[PrimitiveType] = "boolean" ^^^ PrimitiveBoolean
}
sealed trait PrimitiveType
case object PrimitiveInt extends PrimitiveType
case object PrimitiveChar extends PrimitiveType
case object PrimitiveBoolean extends PrimitiveType