Scala 错误的前向引用错误
Scala's wrong forward reference error
private def foo(a:A):B = a match{
case A(...) =>
val x = a.b //error: wrong forward reference a
...
}
A(...) 中未提及 b,如果这很重要的话。
我在 Google 上试过运气,但我似乎只找到了一些人的帖子,这些帖子有涉及前向引用的错误,但没有解释这个特定错误的实际含义。
如果有人能帮助我,我将不胜感激。
问题是您可能以某种错误的方式使用了模式匹配。作为...您没有提供完整的代码。我不知道那个错误是什么。
我确定其他地方存在问题,因为以下代码(与您提供的代码几乎相同)可以完美运行,
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
所以...基本上如果您遇到以下情况class,
case class A( b: String, c: String )
现在以下就可以了。
private def foo( a:A ): B = a match{
case A( iAmB, iAmC ) => {
// iAmB and iAmC have values of a.b and a.c repectively
...
}
}
在你的情况下......你的函数清楚地表明你的 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
...
}
嗯,我现在是不是觉得自己很傻...
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
...
}
因此,简单重命名即可解决问题:
private def foo(aa:A):B = aa match{
case A(...) =>
val x = aa.b
...
val a = ...
...
}
这里试图解释@User1291 没有回答 his/her 的内容。
我是 Scala 的新手,Java 所以答案对我来说并不明显。我很惊讶 运行 在我的(简化)代码中出现这个错误:
object Main {
val data = getData()
def getUser() = {
getUserFrom(data) // error: Wrong Forward Reference
}
}
Wrong Forward Reference 等同于 Java 的 Illegal Forward Reference,这是一种奇特的说法,表示您不能引用在编译时未知的值。在这种情况下,getData()
在 运行 时间内只能 return 值,并且引用 data
给出了这个错误。
当我尝试更改代码以引用已知字符串时,错误消失了:
object Main {
val name = "PieOhPah"
def getUser() = {
getUserFrom(name)
}
}
另一种方法是用函数关闭值并从内部访问它,因为函数直到 运行time:
才会计算
object Main {
val data = getData()
def getUser(userData: UserData) = {
getUserFrom(userData)
}
// Invoke the method later with `data`
print(getUser(data).name)
}
private def foo(a:A):B = a match{
case A(...) =>
val x = a.b //error: wrong forward reference a
...
}
A(...) 中未提及 b,如果这很重要的话。
我在 Google 上试过运气,但我似乎只找到了一些人的帖子,这些帖子有涉及前向引用的错误,但没有解释这个特定错误的实际含义。
如果有人能帮助我,我将不胜感激。
问题是您可能以某种错误的方式使用了模式匹配。作为...您没有提供完整的代码。我不知道那个错误是什么。
我确定其他地方存在问题,因为以下代码(与您提供的代码几乎相同)可以完美运行,
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
所以...基本上如果您遇到以下情况class,
case class A( b: String, c: String )
现在以下就可以了。
private def foo( a:A ): B = a match{
case A( iAmB, iAmC ) => {
// iAmB and iAmC have values of a.b and a.c repectively
...
}
}
在你的情况下......你的函数清楚地表明你的 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
...
}
嗯,我现在是不是觉得自己很傻...
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
...
}
因此,简单重命名即可解决问题:
private def foo(aa:A):B = aa match{
case A(...) =>
val x = aa.b
...
val a = ...
...
}
这里试图解释@User1291 没有回答 his/her 的内容。
我是 Scala 的新手,Java 所以答案对我来说并不明显。我很惊讶 运行 在我的(简化)代码中出现这个错误:
object Main {
val data = getData()
def getUser() = {
getUserFrom(data) // error: Wrong Forward Reference
}
}
Wrong Forward Reference 等同于 Java 的 Illegal Forward Reference,这是一种奇特的说法,表示您不能引用在编译时未知的值。在这种情况下,getData()
在 运行 时间内只能 return 值,并且引用 data
给出了这个错误。
当我尝试更改代码以引用已知字符串时,错误消失了:
object Main {
val name = "PieOhPah"
def getUser() = {
getUserFrom(name)
}
}
另一种方法是用函数关闭值并从内部访问它,因为函数直到 运行time:
才会计算object Main {
val data = getData()
def getUser(userData: UserData) = {
getUserFrom(userData)
}
// Invoke the method later with `data`
print(getUser(data).name)
}