On Thu, Jun 5, 2008 at 9:31 PM, Nils Kilden-Pedersen <
nilskp@...> wrote:
> I was looking at the byte code generated for a lazy val, and had a few
> questions and comments. It appears to be implemented as basic DCL using a
> volatile int as flag and synchronizing on the class instance.
> * The int flag does & operation for every access to lazy val. Why, and why
> not a simple boolean?
> * Synchronization is on the enclosing instance. Is that deliberate? Worst
> case, it could cause a hard to find dead-lock. Could a synthetic monitor not
> be used to eliminate worst case, or am I missing some obvious reason?
The reason in both cases is memory usage - this way there's a fairly
low overhead for using a lazy val.
I actually have an alternative lazy implementation which would solve
both these problems that I want to try out some time. It just requires
me to first get over my fear of the scala compiler.