What does “Forward reference extends over definition of value” mean in Scala?

前端 未结 7 2180
一个人的身影
一个人的身影 2020-12-10 00:20

I keep getting

Forward reference extends over definition of value a

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

相关标签:
7条回答
  • 2020-12-10 00:51

    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.

    0 讨论(0)
  • 2020-12-10 00:55

    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")
    
    0 讨论(0)
  • 2020-12-10 01:02

    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 x appears 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.

    0 讨论(0)
  • 2020-12-10 01:03

    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")
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:11

    An example for the answer given by 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 test is defined after the call and there is a value definition of x in between. Removing/Moving val x definition will solve the problem.

    0 讨论(0)
  • 2020-12-10 01:13

    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.

    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 _)
    
    0 讨论(0)
提交回复
热议问题