Store-passing style is a programming technique that is used to model
mutable state without using mutable state.
It generally arises in the conversion of
imperative programs into
purely functional ones.
So, for instance, consider this
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
program, written in a non-store-passing-style:
var lastWasA = false
// a treebin represents a binary tree of strings.
// a treebin is either
// - a string, or
// -
// does an in-order traversal of this tree's
// leaves contain an 'a' followed by a 'b'?
function aThenB(treebin)
This contains a reference to a
global variable
In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global ...
. In store-passing style, the value of the global variable (or variables) is passed along to each call, and also returned from each call and threaded through the next call. The code might look like this:
function aThenB(treebin, lastWasA)
Note that each call takes an extra argument, and two values are now returned; the ordinary return value, and a new value representing the state of the formerly mutable variable.
Store-passing style can be quite painful to write, but can help to eliminate race conditions by isolating state within function calls, and can potentially make code more
parallelizable
In mathematics, a differentiable manifold M of dimension ''n'' is called parallelizable if there exist Smooth function, smooth vector fields
\
on the manifold, such that at every point p of M the tangent vectors
\
provide a Basis of a vector space, ...
.
See also
*
Continuation-passing style
In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. This is contrasted with direct style, which is the usual style of programming. Gerald Jay S ...
References
{{Reflist
Software design patterns
Functional programming