Home

thunk

A thunk is a programming construct that represents a parameterless function which encapsulates a computation to be performed later. By capturing the necessary environment or arguments, a thunk delays evaluation and allows code to request the result at an indeterminate time. The term originated in early compiler literature, and its exact origin is informal, appearing in the 1960s and 1970s in discussions of delayed computation and call mechanics.

In lazy evaluation and functional languages, thunks implement deferred computation. A thunk wraps an expression so

Thunks are used in compiler design and foreign-function interfaces to adapt calling conventions or environments. A

In contemporary software development, the term appears in other contexts as well. A notable modern example

Overall, thunks provide a simple, flexible mechanism to encapsulate and control the timing of computations, offering

that
evaluating
the
thunk
triggers
the
evaluation
of
the
expression,
sometimes
with
the
result
memoized
for
repeated
use.
Thunks
also
appear
in
continuation-passing
style
and
in
call-by-need
implementations,
where
computation
is
postponed
until
its
value
is
actually
required.
thunk
can
be
a
small
wrapper
that
adjusts
the
this
pointer,
bridges
differences
between
languages
(for
example,
C
and
C++),
or
supplies
missing
arguments,
allowing
a
function
to
be
invoked
through
a
uniform,
generic
entry
point.
is
the
Redux
thunk
middleware
in
JavaScript,
where
a
thunk
is
a
function
returned
by
an
action
creator
that
can
perform
asynchronous
work
and
dispatch
actions
later.
a
bridge
between
eager
execution,
lazy
evaluation,
and
cross-language
calling.