What does a lazy val do?
This feature helps not only delaying expensive calculations, but is also useful to construct mutual dependent or cyclic structures. E.g. this leads to an stack overflow:
trait Foo { val foo: Foo }
case class Fee extends Foo { val foo = Faa() }
case class Faa extends Foo { val foo = Fee() }
println(Fee().foo)
//StackOverflowException
But with lazy vals it works fine
trait Foo { val foo: Foo }
case class Fee extends Foo { lazy val foo = Faa() }
case class Faa extends Foo { lazy val foo = Fee() }
println(Fee().foo)
//Faa()
The difference between them is, that a val
is executed when it is defined whereas a lazy val
is executed when it is accessed the first time.
scala> val x = { println("x"); 15 }
x
x: Int = 15
scala> lazy val y = { println("y"); 13 }
y: Int = <lazy>
scala> x
res2: Int = 15
scala> y
y
res3: Int = 13
scala> y
res4: Int = 13
In contrast to a method (defined with def
) a lazy val
is executed once and then never again. This can be useful when an operation takes long time to complete and when it is not sure if it is later used.
scala> class X { val x = { Thread.sleep(2000); 15 } }
defined class X
scala> class Y { lazy val y = { Thread.sleep(2000); 13 } }
defined class Y
scala> new X
res5: X = X@262505b7 // we have to wait two seconds to the result
scala> new Y
res6: Y = Y@1555bd22 // this appears immediately
Here, when the values x
and y
are never used, only x
unnecessarily wasting resources. If we suppose that y
has no side effects and that we do not know how often it is accessed (never, once, thousands of times) it is useless to declare it as def
since we don't want to execute it several times.
If you want to know how lazy vals
are implemented, see this question.