Skip to content

Lazy vals initialisation can be retried after exception #4451

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

Closed
allanrenucci opened this issue May 3, 2018 · 5 comments
Closed

Lazy vals initialisation can be retried after exception #4451

allanrenucci opened this issue May 3, 2018 · 5 comments

Comments

@allanrenucci
Copy link
Contributor

class Foo(x: Int) {
  private[this] var init = false;
  lazy val foo = {
    if (!init) {
      init = true
      assert(false)
    }
    x
  }
}

object Test {
  def main(args: Array[String]): Unit = {
    val foo = new Foo(42)
    try foo.foo
    catch {
      case _: AssertionError =>
        println(foo.foo) // prints 0, should be 42
    }
  }
}
@smarter
Copy link
Member

smarter commented May 3, 2018

Do we really want to do this? Is this a documented/specified behavior of scalac?

@allanrenucci
Copy link
Contributor Author

@allanrenucci
Copy link
Contributor Author

I found this that says:

The current Scala semantics demand retrying the initialization in case of failure.

@smarter
Copy link
Member

smarter commented May 3, 2018

@DarkDimius Is it intentional that the Dotty implementation of lazy val does not allow re-initialization after an exception?

@allanrenucci allanrenucci self-assigned this May 28, 2018
@allanrenucci
Copy link
Contributor Author

The lazy val is retried if it is annotated @volatile

allanrenucci added a commit that referenced this issue Jul 20, 2018
Fix #4451: Set flag in the correct order
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Jul 23, 2018
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