Alternative lazy evaluation

There is another way to implement lazy evaluations in Scala. It is through using anonymous functions and taking advantage of the fact that functions are a part of unifications in Scala and we can also pass them as parameters easily. This is done as follows—a value is represented as () => value rather than just the value itself. It is somewhat pointless, though, especially because we already have two mechanisms that can do quite a lot. Using anonymous functions for a lazy evaluation is not recommended.

Passing a function to a method can also be considered as a way of lazily evaluating some data. This, however, can be useful and should not be confused with what we just said about anonymous functions.

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.