Explicit Lazy Evaluation

Use of existing language features to emulate LazyEvaluation in an eager programming language. (Most mainstream programming languages, other than the Haskell family, are eager by default. Note that strict evaluation refers to completely evaluating a function's arguments before execution, while eager evaluation refers to evaluation of an expression as soon as it is bound).

The usual technique is to create a function/object which when called (or has a well-known method invoked on it), yields the desired result. The function/object should then save the result away, so that subsequent calls yield the same result. (Otherwise, you have NormalOrderEvaluation). Note that the state transitions required are monotonic, so that this isn't considered too offensive in a SideEffect-free context. (ConceptsTechniquesAndModelsOfComputerProgramming discusses this in great depth).

Note that the function/object must have some state, to record whether or not the result has been evaluated, and to store the result if it has. A standard CeeCeePlusPlus function won't work; a LexicalClosure will (or an "emulated closure", which consists of a function and a pointer to some state). Objects will work fine.

Some languages make this easier than others:

See also ImplicitLazyEvaluation


CategoryLazyPattern


EditText of this page (last edited May 8, 2014) or FindPage with title or text search