ネタ記録庫/Scala/Android/sbt-0.11.2
class Lazy[A](thunk : () => A) { lazy val body = thunk() } object Lazy { def lazy_[A](x : => A) = new Lazy(() => x) def force[A](x : Lazy[A]) = x.body }
import Lazy._ def calc() = { println("calc") 1+2+3 } val x = lazy_(calc()) println("start") println(force(x)) println(force(x)) println(force(x))
実行結果:
start calc 6 6 6