Deferred Desires with Typelevel Cats
Now, later, or always—choose your evaluation kink
The Eval monad in Cats gives developers control over how and when computations are evaluated. Unlike plain Scala expressions, Eval lets you select whether a value should be computed eagerly, lazily with caching, or lazily without caching. It also ensures stack safety for recursive computations. Exploring Eval through practical examples shows how it can be applied in realistic code construction rather than remaining a theoretical construct.
Evaluation Strategies
Eval offers three main strategies. Eval.now is eager and computes the value immediately, keeping it stable forever. Eval.later delays computation until the first access and caches the result for later use. Eval.always recomputes on every access, which makes it useful when the underlying state changes or randomness is involved. In addition, Eval.defer suspends evaluation of another Eval, which is critical for stack safety in recursion. Consider the following code that highlights the different strategies - the code is available in my Github repo.



