什么是“前向参考超出了价值的定义"?在 Scala 中是什么意思? [英] What does "Forward reference extends over definition of value" mean in Scala?

查看:23
本文介绍了什么是“前向参考超出了价值的定义"?在 Scala 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在接受

前向引用扩展了值 a 的定义

尝试编译我的应用程序时出错(在 SBT 中).

a 只是 val a = "",错误是通过访问 a 之前的特定参数(函数的)触发的定义.该参数是一个简单的 case 类类型,具有 Option[...] 类型的所有三个字段(Option[org.joda.time.DateTime] 中的 2 个和 1 个枚举值的选项).

前向引用扩展到价值的定义"到底意味着什么,有什么方法可以对抗它?

解决方案

根据您的 scalac 版本,存在合成方法导致此错误的错误.

https://issues.scala-lang.org/browse/SI-6278

说明,想象f生成:

对象测试{def main(args: Array[String]) {类 NotUsed {val x = f}val 虚拟 = 假def f = 真}}

案例类、默认参数和隐式类涉及合成.

在该票证的示例代码(已修复)中,您可以通过将隐式移动到函数末尾来破坏 ok 方法:

object tiny {def main(args: Array[String]) {好的();不()}def ok() {类 Foo(val i: Int) {def foo[A](body: =>A): A = body}隐式 def toFoo(i: Int): Foo = new Foo(i)值 k = 1k foo println("k?")val j = 2}def nope() {隐式类 Foo(val i: Int) {def foo[A](body: =>A): A = body}值 k = 1k foo println("k?")//懒惰的val j = 2}}

<块引用>

有什么方法可以对抗它?

正如代码中的注释所暗示的那样,使定义延迟是一种解决方法.

图例2,想象一下函数很长以至于你没有注意到命名问题:

对象测试{def main(args: Array[String]) {类 NotUsed {val xs = args}val 虚拟 = 假//哎呀,阴影参数def args = Seq("a","b","c")}}

I keep getting

Forward reference extends over definition of value a

error while trying to compile my application (inside SBT).

a is just val a = "", the error is triggered by accessing a particular parameter (of the function) right before a definition. 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).

What can "Forward reference extends over definition of value" mean at all and what can be the ways to fight it?

解决方案

Depending on your scalac version, there have been bugs where synthetic methods cause this error.

https://issues.scala-lang.org/browse/SI-6278

Illustration, imagine f is generated:

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:

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:

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")
  }
}

这篇关于什么是“前向参考超出了价值的定义"?在 Scala 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆