And, FWIW, one thing we just noticed is that the lazy implementation for
this case takes the lock on the object that contains this val even though
the scope of this particular value is note the object scope, at least in
2.11.something.

In our case, we had a lot of lock contention that we weren't
expecting/aware of.

Our conclusion was that lazy locals are of _very_ limited and mostly to be
avoided.

On Tue, Apr 10, 2018 at 8:41 AM, Adam Mackler <adammack...@gmail.com> wrote:

> 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.
>

-- 
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