Programming with Moose/Problems solved/Scalar-Defer

's  is a method of computing a value once and caching it away, while Memoize is a method of storing the functions input and output into a cache table for the purpose of future calls. Both of these functionalities are largely covered by Moose's native. This deadly combo ( and  ) native to Moose caches a function's output, and delays the execution until the first runtime request.

Procedurally, a function gets arguments or operates on globals -- if it does neither than it is presumably immutable. Object Orientationally, a method often gets its variables from the object (its environment) and sometimes modifies the object. Take for instance a method like, which might check to see if the object can   and then it might shout what the object is. With this in mind, let's see what Memoize does...

"‘Memoizing’ a function makes it faster by trading space for time. It does this by caching the return values of the function in a table. If you call the function again with the same arguments, "memoize" jumps in and gives you the value out of the table, instead of letting the function compute the value all over again."

You might ask, what's wrong with that? Well, for starters it isn't object oriented; and, doing anything with a method reference is ugly and nasty and should be avoided. Moose's Lazy/Default combo replaces a small but significant subset of Memoize's functionality. If you simply want to cache the return of a runtime function then Moose's native functionality might better suite you.

Finally, the purpose of Memoize is always to make something faster and that isn't Moose's goal in any way shape or form. What functionality Moose delivers with /  is for a totally different reason, all of which more so pertains to programmer productivity which is exactly the goal of Moose. The logical drawback of the way Moose goes about this is that the object will cache only one call to the function, requiring many objects for many calls.

As for the, this probably better fits the functionality Moose provides, because the goal of  , directly collides with that of Moose's Lazy/Default. Let's sum up with a list of things the Lazy/Default combo can be said to do.
 * Calculates the function only once
 * Caches the output
 * Defers evaluation until the first call

The old way
I've (Evan Carroll) have personally never seen memoize used before on a method. Not to say it couldn't be done, or hasn't been... But, rather than contrive an example to make Moose more of the win, I'll present you with a typical vanilla Memoize. That goes to say this could be made more similar in theory, but not in practice.

This example will use lazy/default for the purpose of delaying execution and storing the result; it is often used simply for this. However, it is also often used for delaying a database connection until needed in runtime. These are two totally separate applications of the same technology.

Invoking the Moose
Moose is going to get the same effect a slightly different &mdash; object oriented &mdash; way.

Another Example
I feel obligated to show you a very popular application of this technology.

New Syntax
For clarity and those who just can't learn by example:


 * lazy: Lazy can be reduced to a simple: do this at runtime. It permits delayed execution.


 * default: Default is always used in combination with lazy, tells Moose what should happen when the first call to the getter is made. The combination of lazy/default essentially does a few things:
 * 1) Delays execution until runtime
 * 2) Caches away variable for later calls
 * 3) Permits dynamic overload

So if you don't want to the default/lazy combo, just send it something.