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

Forward reference is reported as a warning or an error, depending on context #11712

Open
julienrf opened this issue Aug 27, 2019 · 2 comments

Comments

@julienrf
Copy link

commented Aug 27, 2019

(From this discussion)

The compiler does not behave the same when it analyzes fields of an object and val definitions in a method:

object Main {
  
  val a = b // no compilation error but a warning “Reference to uninitialized value b”
  val b = "foo"
  
  def main(args: Array[String]): Unit = {
    val c = d
//      ^
// error: forward reference extends over definition of value c
    val d = "bar"

    println(a) // null
  }
}

Is there a reason to treat these two cases differently? Maybe that’s because initialization of object fields is harder in general?

@hrhino

This comment has been minimized.

Copy link
Member

commented Aug 27, 2019

Well, in the local variable case there's no way to generate the bytecode given that forward reference (what do we do when we need to load d?), so it has to be an error.

In the latter case I suppose it's at least plausible that you could write something like that which (due to lazy vals or whatever) would actually work? Maybe it should be an error if we can prove it won't hit any false positives.

@hrhino hrhino closed this Aug 27, 2019

@hrhino hrhino reopened this Aug 27, 2019

@som-snytt

This comment has been minimized.

Copy link

commented Aug 27, 2019

Relatedly for reference
#8716
#399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.