What it is good for?

Lazy initialization is particularly useful when initializing an object or variable that would take too long or might not even be needed. Some might say that we can simply use methods, and this is partly true. However, imagine a case where we might need to access a lazily initialized variable/object from multiple methods in different calls to our object. In this case, it is useful to store the result somewhere and just reuse it.

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.