Scala 中的“前向引用扩展了值的定义”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13328502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "Forward reference extends over definition of value" mean in Scala?
提问by Ivan
I keep getting
我不断得到
Forward reference extends over definition of value a
error while trying to compile my application (inside SBT).
尝试编译我的应用程序时出错(在 SBT 中)。
ais just val a = "", the error is triggered by accessing a particular parameter (of the function) right before adefinition. The parameter is of a simple case class type with all three fields of Option[...]type (2 of Option[org.joda.time.DateTime]and 1 of Option of an enumeration value).
a只是val a = "",错误是通过在a定义之前访问(函数的)特定参数来触发的。该参数是一个简单的 case 类类型,具有所有三个Option[...]类型的字段(Option[org.joda.time.DateTime]枚举值的 Option 的2和 1)。
What can "Forward reference extends over definition of value" mean at all and what can be the ways to fight it?
“前向参考扩展到价值的定义”到底意味着什么,有什么方法可以对抗它?
采纳答案by som-snytt
Depending on your scalac version, there have been bugs where synthetic methods cause this error.
根据您的 scalac 版本,存在合成方法导致此错误的错误。
https://issues.scala-lang.org/browse/SI-6278
https://issues.scala-lang.org/browse/SI-6278
Illustration, imagine f is generated:
图示,想象 f 生成:
object Test {
def main(args: Array[String]) {
class NotUsed {val x = f}
val dummy = false
def f = true
}
}
Case classes, default arguments and implicit classes involve synthetics.
案例类、默认参数和隐式类涉及合成。
In the example code from that ticket (which has been fixed), you can break the ok method by moving the implicit to the end of the function:
在该票证的示例代码(已修复)中,您可以通过将隐式移动到函数末尾来破坏 ok 方法:
object tiny {
def main(args: Array[String]) {
ok(); nope()
}
def ok() {
class Foo(val i: Int) {
def foo[A](body: =>A): A = body
}
implicit def toFoo(i: Int): Foo = new Foo(i)
val k = 1
k foo println("k?")
val j = 2
}
def nope() {
implicit class Foo(val i: Int) {
def foo[A](body: =>A): A = body
}
val k = 1
k foo println("k?")
//lazy
val j = 2
}
}
what can be the ways to fight it?
有什么方法可以对抗它?
As implied by the comment in the code, making the definition lazy is a workaround.
正如代码中的注释所暗示的那样,使定义惰性是一种解决方法。
Illustration 2, imagine the function is so long that you don't notice the naming problem:
图 2,想象一下函数太长以至于你没有注意到命名问题:
object Test {
def main(args: Array[String]) {
class NotUsed {val xs = args}
val dummy = false
// oops, shadows the parameter
def args = Seq("a","b","c")
}
}
回答by sepp2k
The error message means that you have a forward reference to a method, i.e. you're calling a method before you define it, and that the definition of the value xappears between that forward reference and the definition of the method. It is only legal to have forward references if there are no value definition between the reference and the referred method definiton.
错误消息意味着您有一个方法的前向引用,即您在定义之前调用了一个方法,并且值的定义x出现在前向引用和方法的定义之间。只有在引用和被引用的方法定义之间没有值定义时,才有前向引用才是合法的。
回答by Ravi Macha
Basically it's a bug.
基本上这是一个错误。
The fix is to declare a method before calling it. I don't know why.
解决方法是在调用之前声明一个方法。我不知道为什么。
def a(input: String){
}
val k = a("ravi")
回答by freekh
If it is referenced somewhere and scalac is confused with the sequence, making it lazy could do the trick
如果它在某处被引用并且 scalac 与序列混淆,那么让它变得懒惰就可以解决问题
I guess it might be to late for an answer, and since I cannot see what you are actually trying to do I am not sure if that solves it though.
我想答案可能为时已晚,而且由于我看不到您实际上要做什么,因此我不确定这是否能解决问题。
回答by vishnu viswanath
An example for the answer given by sepp2k
sepp2k给出的答案示例
object ForwardReferenceTest {
def main(args: Array[String]): Unit = {
test
val x = 1
def test = println("hi")
}
}
you will get error
你会得到错误
Error:(7, 5) forward reference extends over definition of value x
test
^
the function testis defined after the call and there is a value definition of xin between. Removing/Moving val xdefinition will solve the problem.
函数test是在调用之后定义的x,中间有一个值定义。删除/移动val x定义将解决问题。
回答by Kodebetter
The message means that at run time scala was not able to find the reference of the method that is called in your method. This usually happens when you try to cal a method and the implementation of the called method is after the calling method.
该消息意味着在运行时 scala 无法找到在您的方法中调用的方法的引用。这通常发生在您尝试调用方法并且被调用方法的实现在调用方法之后。
like for ex:
比如前:
implicit val userDetails: Reads[UserDetails] = (
(JsPath \ "name").read[String] and
(JsPath \ "providers").read[List[Provider]]
)(UserDetails.apply _)
implicit val providers: Reads[Provider] = (
(JsPath \ "providerName").read[String] and
(JsPath \ "providerUserId").read[String] and
(JsPath \ "authtoken").read[String]
)(Provider.apply _)
and the correct way to it.
以及正确的方法。
implicit val providers: Reads[Provider] = (
(JsPath \ "providerName").read[String] and
(JsPath \ "providerUserId").read[String] and
(JsPath \ "authtoken").read[String]
)(Provider.apply _)
implicit val userDetails: Reads[UserDetails] = (
(JsPath \ "name").read[String] and
(JsPath \ "providers").read[List[Provider]]
)(UserDetails.apply _)
回答by Samarth
You Should check your imports. It must be importing variable name from the import, The variable name must be used in some library which is imported to your code. Remove the import.
你应该检查你的进口。它必须从导入中导入变量名,变量名必须在导入代码的某个库中使用。删除导入。

