すずけんメモ

技術メモです

lazyによる遅延評価

-> % scala
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> lazy val hoge = {println("hogehoge"); "I'm hoge."}
hoge: String = <lazy>

scala> hoge
hogehoge
res0: String = I'm hoge.

scala> hoge
res1: String = I'm hoge.

scala> hoge
res2: String = I'm hoge.

scala>

初回呼び出しの時に評価が行われる。2回目の呼び出しでは値の評価は行われない。