fork download
  1. class Lazy[A](thunk : () => A) {
  2. lazy val body = thunk()
  3. }
  4.  
  5. object Lazy {
  6. def lazy_[A](x : => A) = new Lazy(() => x)
  7. def force[A](x : Lazy[A]) = x.body
  8. }
  9.  
  10. object Main {
  11. def getX() = {
  12. println("hello")
  13. 3
  14. }
  15. def main(args: Array[String]) {
  16. import Lazy._
  17. val x = lazy_(getX())
  18. println("start")
  19. println(x)
  20. println(x)
  21. println(x)
  22. }
  23. }
Success #stdin #stdout 0.15s 211584KB
stdin
Standard input is empty
stdout
start
Lazy@2e7263
Lazy@2e7263
Lazy@2e7263