Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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?

like image 550
Ivan Avatar asked Nov 11 '12 04:11

Ivan


4 Answers

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.

like image 92
sepp2k Avatar answered Nov 12 '22 00:11

sepp2k


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")
like image 35
Ravi Macha Avatar answered Nov 12 '22 00:11

Ravi Macha


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")
  }
}
like image 15
som-snytt Avatar answered Nov 12 '22 01:11

som-snytt


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.

like image 10
freekh Avatar answered Nov 12 '22 00:11

freekh