The value cached is defined inside the definition of the method func(), so 
everytime you invoke func() it defines a new local Int named cached.  
Change your code to this:

def func(i: => Int): Unit = {
  lazy val cached = {
    println("Calculating")
    i * i
  }
  println(cached)
  println(cached)
  println(cached)
}


and you will see that it prints "Calculating" only once each time you 
invoke func(), even though func() is now accessing cached three times.

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to