我有下面的代碼被編譯反對斯卡拉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」的返回值?
啊,很好的答案。我正在收回我的。 – 2010-08-05 10:43:36
謝謝,當然這解決了我的問題。但是,我仍然缺少爲什麼在|中需要U>:T。 – 2010-08-05 16:24:04
@kmels'U'必須是'T'的超類型,因爲'|'構造了一個複合解析器,它將產生'T'或'U'。在實踐中,給定'T | S',編譯器將在類型層次結構中搜索「T」和「S」的最小上界*,並調用「U」。 – 2010-08-05 18:05:13