方法“|”中解析器的预期类型
我针对 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”的返回值应该是什么?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将最后一行更改为
Thisallows for PrimitiveType[Int] <: PrimitiveType[AnyVal] 当您想通过 | 合并 PrimitiveType[Boolean] 和 PrimitiveType[Int] 解析器的结果时需要它。
顺便说一句,我还建议写成
not ,
因为这对你的情况来说更准确。
You need to change your last line into
This allows for PrimitiveType[Int] <: PrimitiveType[AnyVal] which is needed when you want to merge the results of PrimitiveType[Boolean] and PrimitiveType[Int] parsers via |.
BTW, I would also suggest to write
instead of
since this is more precise in your case.
您可能最好将通用
case class PrimitiveType[T]
更改为类层次结构。泛型类型参数在运行时不可用,因此您将无法对解析结果做太多事情...这将为您提供以下内容(
未测试的代码现已测试):You might be better changing your generic
case class PrimitiveType[T]
to a class hierarchy. The generic type parameters aren't available at runtime, so you won't be able to do much with your parse results...This would give you the following (
untested codenow tested):