What does "Forward reference extends over definition of value" mean in Scala?
Asked Answered
P

9

49

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?

Pomade answered 11/11, 2012 at 4:9 Comment(0)
M
15

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")
  }
}
Mag answered 11/11, 2012 at 5:56 Comment(7)
"Affects Version/s: Unreleased-2.10.x, Fix Version/s: Unreleased-2.10.x" - it says. Do I understand correct that the bug is not fixed in any released version, even in 2.10RC2?Pomade
Indeed. I've just tried 2.10RC2 and the trouble is still there... :-(Pomade
I have even made the class a bare class (not a case class) and removed all parameters defaults from it but I still get the error. My case is much simpler - I just try to access a method parameter and it triggers the error. I still can't get how is this particular parameter of any special.Pomade
Why not paste the sample so we can see in particular? It can also help to use -Xprint:typer (or sometimes a phase after typer).Mag
I have found the actual problem cause (thanks to IntelliJ Idea showing a value declaration when you hold Ctrl and hover mouse over the value usage)! A val with the same name (like the function parameter) was declared later in the same function. So it's not the complier bug, it's a bug of mine.Pomade
Ha. Crowd-sourcing it on SO would have been faster than IntelliJ. :)Mag
Do you mind adding something like "The problem can be also caused by the same-name (as the function argument) value declared later in the function code so that the argument gets hidden being replaced with a value which is not actually accessible." into the answer body to make it more complete?Pomade
B
54

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.

Brocatel answered 11/11, 2012 at 4:34 Comment(2)
Can you provide an example of a valid forward reference?Maurine
magic, i rearranged my method and the compilation error vanishedTabitha
W
19

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")
Willywilly answered 5/4, 2016 at 0:0 Comment(1)
Or make the value lazyGermin
M
15

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")
  }
}
Mag answered 11/11, 2012 at 5:56 Comment(7)
"Affects Version/s: Unreleased-2.10.x, Fix Version/s: Unreleased-2.10.x" - it says. Do I understand correct that the bug is not fixed in any released version, even in 2.10RC2?Pomade
Indeed. I've just tried 2.10RC2 and the trouble is still there... :-(Pomade
I have even made the class a bare class (not a case class) and removed all parameters defaults from it but I still get the error. My case is much simpler - I just try to access a method parameter and it triggers the error. I still can't get how is this particular parameter of any special.Pomade
Why not paste the sample so we can see in particular? It can also help to use -Xprint:typer (or sometimes a phase after typer).Mag
I have found the actual problem cause (thanks to IntelliJ Idea showing a value declaration when you hold Ctrl and hover mouse over the value usage)! A val with the same name (like the function parameter) was declared later in the same function. So it's not the complier bug, it's a bug of mine.Pomade
Ha. Crowd-sourcing it on SO would have been faster than IntelliJ. :)Mag
Do you mind adding something like "The problem can be also caused by the same-name (as the function argument) value declared later in the function code so that the argument gets hidden being replaced with a value which is not actually accessible." into the answer body to make it more complete?Pomade
S
10

If it is referenced somewhere and scalac is confused with the sequence, making it lazy could do the trick

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.

Snaggy answered 1/3, 2013 at 14:2 Comment(0)
E
2

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.

Epicycle answered 23/6, 2016 at 17:22 Comment(0)
S
1

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 _)
Sporangium answered 21/9, 2016 at 18:55 Comment(0)
W
0

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.

Wein answered 10/4, 2020 at 5:56 Comment(0)
L
0

In my case it was enough to move val definition after the implicit val definition, not clear why. Method that was assigned to the val definition was actually defined before the methd where it was actually called.

Leveroni answered 23/11, 2021 at 13:58 Comment(0)
T
0

This error happens when we refer to a variable before it is defined. As the Error states it is Forward Reference.

Tedium answered 5/10, 2023 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.