Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: forward reference extends over definition of value only when inside a block #8551

Closed
scabug opened this issue Apr 30, 2014 · 2 comments

Comments

@scabug
Copy link

scabug commented Apr 30, 2014

Here is the block of code to reproduce the problem:

object TestIt extends App { 

 { // if you remove this bracket block, the compiler does not complain
   def memoize1[A, B](f: A => B): Function1[A, B] = new Function1[A, B] {
     val results = collection.mutable.Map.empty[A, B]
     def apply(in: A) = results.getOrElseUpdate(in, f(in))
   }

   val maxLatency1: Function1[(Int, Int), Int] = memoize1 { t: (Int, Int) =>
     val (s, d) = t
     if (s == d) 0
     else {
       maxLatency1((1, 2))
     }
   }
 } // end of block

}
@scabug
Copy link
Author

scabug commented Apr 30, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8551?orig=1
Reporter: Pierre Schaus (pschaus)
Affected Versions: 2.11.0

@scabug
Copy link
Author

scabug commented Apr 30, 2014

@retronym said:
This is in line with the language spec.

object Test {
  def foo {
   val foo: Any = foo
   val bar: Any = () = bar
  }
}

You can make maxLatency a lazy val to make this allowed.

 The scope of a name introduced by a declaration or definition is the whole statement
 sequence containing the binding. However, there is a restriction on forward references in blocks: 
  In a statement sequence s1 . . . sn making up a block, if a simple name in si refers to an entity
  defined by s j where j >= i , then for all sk between and including si and sj,
    * sk cannot be a variable definition.
    * If sk is a value definition, it must be lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants