Parsing scalac缺少右大括号错误报告,行号怪异
在学习Scala的过程中,当我从Parsing scalac缺少右大括号错误报告,行号怪异,parsing,scala,compiler-construction,scalac,Parsing,Scala,Compiler Construction,Scalac,在学习Scala的过程中,当我从scalac读取神秘的输出时,我经常会想起g++编译器错误。然而,今天我遇到了一些事情,我怀疑即使在g++宇宙中也会发生 一位朋友给我发了一个非常简单的代码片段,其中有一个非常常见的错误: case class Var(name: String) extends ArithExpr { override def eval(env: Env) = env.lookup(name) match { case Some(d) => d cas
scalac
读取神秘的输出时,我经常会想起g++
编译器错误。然而,今天我遇到了一些事情,我怀疑即使在g++
宇宙中也会发生
一位朋友给我发了一个非常简单的代码片段,其中有一个非常常见的错误:
case class Var(name: String) extends ArithExpr {
override def eval(env: Env) = env.lookup(name) match {
case Some(d) => d
case None => throw new IllegalArgumentException("Env " + env + " does not contain a binding for " + name)
}
override def exprString = name
// } // <-- MISSING THIS BRACE
很明显,我们缺少一个右括号。我可以理解scalac
猜测丢失的大括号可能会出现在以下任何一个地方(每个地方都用x
表示):
输入的那部分已经嵌套好了!在那里添加另一个右括号有什么意义
编辑:我觉得我应该再次重申我的主要问题:您如何向Scala的新来者解释此错误消息(以及如何在源代码中找到错误的根源?考虑以下示例(我故意删除缩进): 它不会编译,但有几种可能的正确代码:
case class Foo( i: Int ) {
}
case class Bar( d: Double ) {
def get = d
}
//OR
case class Foo( i: Int ) {
case class Bar( d: Double ) {
def get = d
}
}
//OR even (still won't compile but the structure is correct so the compiler will proceed with
//another error)
case class Foo( i: Int ) {
case class Bar( d: Double ) { }
def get = d
}
那么编译器应该如何猜测哪个版本是正确的呢?在这种情况下,它会选择第一个有意义的位置:
hello.scala:3: error: Missing closing brace `}' assumed here
def get = d
这与第三个选项相对应。根据Jörg W Mittag在他对我的问题的评论中的建议,我在Scala问题跟踪程序中重新打开了一张旧票:。我添加了这个问题中的代码,作为此类令人困惑的错误消息的一个新的、简短的、可复制的示例。这在您给出的示例中是有意义的,但与我发布的代码不同。我更新了我的问题,使之更清楚。-1因为尽管你的答案信息丰富,但它实际上并没有解决我发布的代码中奇怪的行号。顺便说一下:Scala团队认为好的编译器错误消息非常重要。事实上,如果他们不能在保持良好的错误消息的同时想出如何实现这些功能,他们就不会向语言中添加有用的功能。因此,您肯定应该在scala开发人员邮件列表中提出这个问题,并可能在@Jörg W Mittag中提交一个bug-谢谢您的输入。在我在这里发布后,我意识到邮件列表可能是一个更好的地方,但因为我已经发布了,我想我会等待,看看我是否在这里得到了一个好的答案,然后再双重发布到邮件列表。我喜欢Scala,但我认为从Java的转换非常重要。我很高兴听到社区如此强调清晰的编译器错误消息。再次感谢你的建议!
+----- I THINK YOU ARE MISSING A
| CLOSING BRACE RIGHT HERE!
1{ V 4}
6{ x 9}
11{ 12{ 15}
19{ 22}
24{ 26}
28{ 33}
case class Foo( i: Int ) {
case class Bar( d: Double ) {
def get = d
}
case class Foo( i: Int ) {
}
case class Bar( d: Double ) {
def get = d
}
//OR
case class Foo( i: Int ) {
case class Bar( d: Double ) {
def get = d
}
}
//OR even (still won't compile but the structure is correct so the compiler will proceed with
//another error)
case class Foo( i: Int ) {
case class Bar( d: Double ) { }
def get = d
}
hello.scala:3: error: Missing closing brace `}' assumed here
def get = d