Scala 的错误前向引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29274198/
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
Scala's wrong forward reference error
提问by User1291
private def foo(a:A):B = a match{
case A(...) =>
val x = a.b //error: wrong forward reference a
...
}
Where b is not mentioned in A(...), if that matters.
如果 A(...) 中没有提到 b,如果这很重要的话。
I've tried my luck on Google, but I seem to find only posts of people having errors involving forward references but no explanation of what this particular error actually means.
我在谷歌上试过运气,但我似乎只找到了涉及前向引用的错误的人的帖子,但没有解释这个特定错误的实际含义。
Would appreciate it if somebody could help me out.
如果有人可以帮助我,我将不胜感激。
回答by User1291
Well, don't I feel stupid now...
好吧,我现在是不是觉得自己很愚蠢......
private def foo(a:A):B = a match{
case A(...) =>
val x = a.b //error: wrong forward reference a
...
val a = ... //<-- THAT's the reason for the error
...
}
So a simple rename will resolve the issue:
所以一个简单的重命名将解决这个问题:
private def foo(aa:A):B = aa match{
case A(...) =>
val x = aa.b
...
val a = ...
...
}
回答by Pie 'Oh' Pah
Here is an attempt to explain what @User1291 had not with his/her answer.
这是试图解释@User1291 没有用他/她的回答。
I'm new to Scala and Java so the answer wasn't obvious to me. I was surprised to run into this error in my (simplified) code:
我是 Scala 和 Java 的新手,所以答案对我来说并不明显。我很惊讶在我的(简化的)代码中遇到了这个错误:
object Main {
val data = getData()
def getUser() = {
getUserFrom(data) // error: Wrong Forward Reference
}
}
Wrong Forward Reference is equivalent to Java's Illegal Forward Reference, which is a fancy way of saying you can't reference a value that isn't known at compile time. In this case, getData()can only return value during run time, and referencing datagave this error.
错误的前向引用相当于 Java 的非法前向引用,这是一种奇特的方式,表示您不能引用在编译时未知的值。在这种情况下,getData()只能在运行时返回值,引用时会出现data此错误。
When I tried changing the code to reference a known string, as expected the error went away:
当我尝试更改代码以引用已知字符串时,正如预期的那样,错误消失了:
object Main {
val name = "PieOhPah"
def getUser() = {
getUserFrom(name)
}
}
Another way is to close over the value with a function and access it from inside since functions are not evaluated until runtime:
另一种方法是用函数关闭值并从内部访问它,因为函数直到运行时才会被评估:
object Main {
val data = getData()
def getUser(userData: UserData) = {
getUserFrom(userData)
}
// Invoke the method later with `data`
print(getUser(data).name)
}
回答by Sarvesh Kumar Singh
The problem is that you are probably using pattern-matching in some wrong way. As... You have not provided complete code. I have no idea about what is that mistake.
问题是您可能以某种错误的方式使用模式匹配。作为...您没有提供完整的代码。我不知道那个错误是什么。
I am sure there is a problem somewhere else as following code (which is almost same as what you have given ) works flawlessly,
我确信其他地方存在问题,因为以下代码(与您给出的代码几乎相同)可以完美运行,
scala> :pa
// Entering paste mode (ctrl-D to finish)
case class A( c: String ) {
val b: String = c
}
def demoA( a: A ): String = a match {
case A( iAmC ) => {
val x = a.b
x
}
}
// Exiting paste mode, now interpreting.
defined class A
demoA: (a: A)String
scala> val anA = A( "sdfsd" )
anA: A = A(sdfsd)
scala> demoA( anA )
res3: String = sdfsd
So... basically if you have a case class like following,
所以......基本上如果你有一个像下面这样的案例类,
case class A( b: String, c: String )
Now following would have worked.
现在下面会起作用。
private def foo( a:A ): B = a match{
case A( iAmB, iAmC ) => {
// iAmB and iAmC have values of a.b and a.c repectively
...
}
}
In your case...your function clearly says that your ais an instance of A- def foo( a:A )so... you really don't need to pattern match here.
在你的情况下......你的函数清楚地表明你a是一个实例A-def foo( a:A )所以......你真的不需要在这里进行模式匹配。
private def foo( a:A ): B = {
// Now class A should have member b and c
val iAmB = a.b
val iAmC = a.c
...
}

