Skip to content

Lazy vals initialisation can be retried after exception #4451

Closed
@allanrenucci

Description

@allanrenucci
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
    }
  }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions