<s>
In	O
programming	O
languages	O
,	O
a	O
closure	B-Language
,	O
also	O
lexical	B-Language
closure	I-Language
or	O
function	B-Language
closure	I-Language
,	O
is	O
a	O
technique	O
for	O
implementing	O
lexically	O
scoped	O
name	O
binding	O
in	O
a	O
language	O
with	O
first-class	B-Application
functions	I-Application
.	O
</s>
<s>
Operationally	O
,	O
a	O
closure	B-Language
is	O
a	O
record	O
storing	O
a	O
function	O
together	O
with	O
an	O
environment	O
.	O
</s>
<s>
The	O
environment	O
is	O
a	O
mapping	O
associating	O
each	O
free	O
variable	O
of	O
the	O
function	O
(	O
variables	O
that	O
are	O
used	O
locally	O
,	O
but	O
defined	O
in	O
an	O
enclosing	O
scope	B-Language
)	O
with	O
the	O
value	O
or	O
reference	O
to	O
which	O
the	O
name	O
was	O
bound	O
when	O
the	O
closure	B-Language
was	O
created	O
.	O
</s>
<s>
Unlike	O
a	O
plain	O
function	O
,	O
a	O
closure	B-Language
allows	O
the	O
function	O
to	O
access	O
those	O
captured	O
variables	O
through	O
the	O
closure	B-Language
's	O
copies	O
of	O
their	O
values	O
or	O
references	O
,	O
even	O
when	O
the	O
function	O
is	O
invoked	O
outside	O
their	O
scope	B-Language
.	O
</s>
<s>
The	O
concept	O
of	O
closures	B-Language
was	O
developed	O
in	O
the	O
1960s	O
for	O
the	O
mechanical	O
evaluation	O
of	O
expressions	O
in	O
the	O
λ-calculus	B-Language
and	O
was	O
first	O
fully	O
implemented	O
in	O
1970	O
as	O
a	O
language	O
feature	O
in	O
the	O
PAL	B-Language
programming	O
language	O
to	O
support	O
lexically	O
scoped	O
first-class	B-Application
functions	I-Application
.	O
</s>
<s>
Peter	O
Landin	O
defined	O
the	O
term	O
closure	B-Language
in	O
1964	O
as	O
having	O
an	O
environment	O
part	O
and	O
a	O
control	O
part	O
as	O
used	O
by	O
his	O
SECD	B-Application
machine	I-Application
for	O
evaluating	O
expressions	O
.	O
</s>
<s>
Joel	O
Moses	O
credits	O
Landin	O
with	O
introducing	O
the	O
term	O
closure	B-Language
to	O
refer	O
to	O
a	O
lambda	B-General_Concept
expression	I-General_Concept
which	O
open	O
bindings	O
(	O
free	O
variables	O
)	O
have	O
been	O
closed	O
by	O
(	O
or	O
bound	O
in	O
)	O
the	O
lexical	B-Language
environment	I-Language
,	O
resulting	O
in	O
a	O
closed	O
expression	O
,	O
or	O
closure	B-Language
.	O
</s>
<s>
This	O
use	O
was	O
subsequently	O
adopted	O
by	O
Sussman	O
and	O
Steele	O
when	O
they	O
defined	O
Scheme	B-Language
in	O
1975	O
,	O
a	O
lexically	O
scoped	O
variant	O
of	O
Lisp	B-Language
,	O
and	O
became	O
widespread	O
.	O
</s>
<s>
Sussman	O
and	O
Abelson	O
also	O
use	O
the	O
term	O
closure	B-Language
in	O
the	O
1980s	O
with	O
a	O
second	O
,	O
unrelated	O
meaning	O
:	O
the	O
property	O
of	O
an	O
operator	O
that	O
adds	O
data	O
to	O
a	O
data	B-General_Concept
structure	I-General_Concept
to	O
also	O
be	O
able	O
to	O
add	O
nested	O
data	B-General_Concept
structures	I-General_Concept
.	O
</s>
<s>
The	O
term	O
closure	B-Language
is	O
often	O
used	O
as	O
a	O
synonym	O
for	O
anonymous	B-General_Concept
function	I-General_Concept
,	O
though	O
strictly	O
,	O
an	O
anonymous	B-General_Concept
function	I-General_Concept
is	O
a	O
function	B-General_Concept
literal	I-General_Concept
without	O
a	O
name	O
,	O
while	O
a	O
closure	B-Language
is	O
an	O
instance	O
of	O
a	O
function	O
,	O
a	O
value	O
,	O
whose	O
non-local	O
variables	O
have	O
been	O
bound	O
either	O
to	O
values	O
or	O
to	O
storage	O
locations	O
(	O
depending	O
on	O
the	O
language	O
;	O
see	O
the	O
lexical	B-Language
environment	I-Language
section	O
below	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
following	O
Python	B-Language
code	I-Language
:	O
</s>
<s>
the	O
values	O
of	O
a	O
and	O
b	O
are	O
closures	B-Language
,	O
in	O
both	O
cases	O
produced	O
by	O
returning	O
a	O
nested	O
function	O
with	O
a	O
free	O
variable	O
from	O
the	O
enclosing	O
function	O
,	O
so	O
that	O
the	O
free	O
variable	O
binds	O
to	O
the	O
value	O
of	O
parameter	O
x	O
of	O
the	O
enclosing	O
function	O
.	O
</s>
<s>
The	O
closures	B-Language
in	O
a	O
and	O
b	O
are	O
functionally	O
identical	O
.	O
</s>
<s>
The	O
only	O
difference	O
in	O
implementation	O
is	O
that	O
in	O
the	O
first	O
case	O
we	O
used	O
a	O
nested	O
function	O
with	O
a	O
name	O
,	O
g	O
,	O
while	O
in	O
the	O
second	O
case	O
we	O
used	O
an	O
anonymous	O
nested	O
function	O
(	O
using	O
the	O
Python	B-Language
keyword	O
lambda	B-Language
for	O
creating	O
an	O
anonymous	B-General_Concept
function	I-General_Concept
)	O
.	O
</s>
<s>
A	O
closure	B-Language
is	O
a	O
value	O
like	O
any	O
other	O
value	O
.	O
</s>
<s>
This	O
usage	O
may	O
be	O
deemed	O
an	O
"	O
anonymous	B-General_Concept
closure	I-General_Concept
"	O
.	O
</s>
<s>
The	O
nested	O
function	O
definitions	O
are	O
not	O
themselves	O
closures	B-Language
:	O
they	O
have	O
a	O
free	O
variable	O
which	O
is	O
not	O
yet	O
bound	O
.	O
</s>
<s>
Only	O
once	O
the	O
enclosing	O
function	O
is	O
evaluated	O
with	O
a	O
value	O
for	O
the	O
parameter	O
is	O
the	O
free	O
variable	O
of	O
the	O
nested	O
function	O
bound	O
,	O
creating	O
a	O
closure	B-Language
,	O
which	O
is	O
then	O
returned	O
from	O
the	O
enclosing	O
function	O
.	O
</s>
<s>
Lastly	O
,	O
a	O
closure	B-Language
is	O
only	O
distinct	O
from	O
a	O
function	O
with	O
free	O
variables	O
when	O
outside	O
of	O
the	O
scope	B-Language
of	O
the	O
non-local	O
variables	O
,	O
otherwise	O
the	O
defining	O
environment	O
and	O
the	O
execution	O
environment	O
coincide	O
and	O
there	O
is	O
nothing	O
to	O
distinguish	O
these	O
(	O
static	O
and	O
dynamic	O
binding	O
cannot	O
be	O
distinguished	O
because	O
the	O
names	O
resolve	O
to	O
the	O
same	O
values	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
below	O
program	O
,	O
functions	O
with	O
a	O
free	O
variable	O
x	O
(	O
bound	O
to	O
the	O
non-local	O
variable	O
x	O
with	O
global	O
scope	B-Language
)	O
are	O
executed	O
in	O
the	O
same	O
environment	O
where	O
x	O
is	O
defined	O
,	O
so	O
it	O
is	O
immaterial	O
whether	O
these	O
are	O
actually	O
closures	B-Language
:	O
</s>
<s>
This	O
is	O
most	O
often	O
achieved	O
by	O
a	O
function	O
return	O
,	O
since	O
the	O
function	O
must	O
be	O
defined	O
within	O
the	O
scope	B-Language
of	O
the	O
non-local	O
variables	O
,	O
in	O
which	O
case	O
typically	O
its	O
own	O
scope	B-Language
will	O
be	O
smaller	O
.	O
</s>
<s>
This	O
can	O
also	O
be	O
achieved	O
by	O
variable	B-Language
shadowing	I-Language
(	O
which	O
reduces	O
the	O
scope	B-Language
of	O
the	O
non-local	O
variable	O
)	O
,	O
though	O
this	O
is	O
less	O
common	O
in	O
practice	O
,	O
as	O
it	O
is	O
less	O
useful	O
and	O
shadowing	O
is	O
discouraged	O
.	O
</s>
<s>
In	O
this	O
example	O
f	O
can	O
be	O
seen	O
to	O
be	O
a	O
closure	B-Language
because	O
x	O
in	O
the	O
body	O
of	O
f	O
is	O
bound	O
to	O
the	O
x	O
in	O
the	O
global	O
namespace	O
,	O
not	O
the	O
x	O
local	O
to	O
g	O
:	O
</s>
<s>
The	O
use	O
of	O
closures	B-Language
is	O
associated	O
with	O
languages	O
where	O
functions	O
are	O
first-class	O
objects	O
,	O
in	O
which	O
functions	O
can	O
be	O
returned	O
as	O
results	O
from	O
higher-order	B-Language
functions	I-Language
,	O
or	O
passed	O
as	O
arguments	O
to	O
other	O
function	O
calls	O
;	O
if	O
functions	O
with	O
free	O
variables	O
are	O
first-class	O
,	O
then	O
returning	O
one	O
creates	O
a	O
closure	B-Language
.	O
</s>
<s>
Julia	B-Application
,	O
</s>
<s>
Rust	B-Application
.	O
</s>
<s>
Closures	B-Language
are	O
also	O
frequently	O
used	O
with	O
callbacks	O
,	O
particularly	O
for	O
event	O
handlers	O
,	O
such	O
as	O
in	O
JavaScript	B-Language
,	O
where	O
they	O
are	O
used	O
for	O
interactions	O
with	O
a	O
dynamic	O
web	O
page	O
.	O
</s>
<s>
Closures	B-Language
can	O
also	O
be	O
used	O
in	O
a	O
continuation-passing	B-Application
style	I-Application
to	O
hide	B-Application
state	I-Application
.	O
</s>
<s>
Constructs	O
such	O
as	O
objects	O
and	O
control	O
structures	O
can	O
thus	O
be	O
implemented	O
with	O
closures	B-Language
.	O
</s>
<s>
In	O
some	O
languages	O
,	O
a	O
closure	B-Language
may	O
occur	O
when	O
a	O
function	O
is	O
defined	O
within	O
another	O
function	O
,	O
and	O
the	O
inner	O
function	O
refers	O
to	O
local	O
variables	O
of	O
the	O
outer	O
function	O
.	O
</s>
<s>
At	O
run-time	B-Library
,	O
when	O
the	O
outer	O
function	O
executes	O
,	O
a	O
closure	B-Language
is	O
formed	O
,	O
consisting	O
of	O
the	O
inner	O
function	O
's	O
code	O
and	O
references	O
(	O
the	O
upvalues	B-Language
)	O
to	O
any	O
variables	O
of	O
the	O
outer	O
function	O
required	O
by	O
the	O
closure	B-Language
.	O
</s>
<s>
Closures	B-Language
typically	O
appear	O
in	O
languages	O
with	O
first-class	B-Application
functions	I-Application
—	O
in	O
other	O
words	O
,	O
such	O
languages	O
enable	O
functions	O
to	O
be	O
passed	O
as	O
arguments	O
,	O
returned	O
from	O
function	O
calls	O
,	O
bound	O
to	O
variable	O
names	O
,	O
etc.	O
,	O
just	O
like	O
simpler	O
types	O
such	O
as	O
strings	O
and	O
integers	O
.	O
</s>
<s>
For	O
example	O
,	O
consider	O
the	O
following	O
Scheme	B-Language
function	O
:	O
</s>
<s>
In	O
this	O
example	O
,	O
the	O
lambda	B-General_Concept
expression	I-General_Concept
(	O
lambda	B-Language
(	O
book	O
)	O
(>=	O
(	O
book-sales	O
book	O
)	O
threshold	O
)	O
)	O
appears	O
within	O
the	O
function	O
best-selling-books	O
.	O
</s>
<s>
When	O
the	O
lambda	B-General_Concept
expression	I-General_Concept
is	O
evaluated	O
,	O
Scheme	B-Language
creates	O
a	O
closure	B-Language
consisting	O
of	O
the	O
code	O
for	O
the	O
lambda	B-General_Concept
expression	I-General_Concept
and	O
a	O
reference	O
to	O
the	O
threshold	O
variable	O
,	O
which	O
is	O
a	O
free	O
variable	O
inside	O
the	O
lambda	B-General_Concept
expression	I-General_Concept
.	O
</s>
<s>
The	O
closure	B-Language
is	O
then	O
passed	O
to	O
the	O
filter	O
function	O
,	O
which	O
calls	O
it	O
repeatedly	O
to	O
determine	O
which	O
books	O
are	O
to	O
be	O
added	O
to	O
the	O
result	O
list	O
and	O
which	O
are	O
to	O
be	O
discarded	O
.	O
</s>
<s>
Because	O
the	O
closure	B-Language
itself	O
has	O
a	O
reference	O
to	O
threshold	O
,	O
it	O
can	O
use	O
that	O
variable	O
each	O
time	O
filter	O
calls	O
it	O
.	O
</s>
<s>
Here	O
is	O
the	O
same	O
example	O
rewritten	O
in	O
JavaScript	B-Language
,	O
another	O
popular	O
language	O
with	O
support	O
for	O
closures	B-Language
:	O
</s>
<s>
The	O
function	O
keyword	O
is	O
used	O
here	O
instead	O
of	O
lambda	B-Language
,	O
and	O
an	O
Array.filter	O
method	O
instead	O
of	O
a	O
global	O
filter	O
function	O
,	O
but	O
otherwise	O
the	O
structure	O
and	O
the	O
effect	O
of	O
the	O
code	O
are	O
the	O
same	O
.	O
</s>
<s>
A	O
function	O
may	O
create	O
a	O
closure	B-Language
and	O
return	O
it	O
,	O
as	O
in	O
this	O
example	O
:	O
</s>
<s>
Because	O
the	O
closure	B-Language
in	O
this	O
case	O
outlives	O
the	O
execution	O
of	O
the	O
function	O
that	O
creates	O
it	O
,	O
the	O
variables	O
f	O
and	O
dx	O
live	O
on	O
after	O
the	O
function	O
derivative	O
returns	O
,	O
even	O
though	O
execution	O
has	O
left	O
their	O
scope	B-Language
and	O
they	O
are	O
no	O
longer	O
visible	O
.	O
</s>
<s>
In	O
languages	O
without	O
closures	B-Language
,	O
the	O
lifetime	O
of	O
an	O
automatic	O
local	O
variable	O
coincides	O
with	O
the	O
execution	O
of	O
the	O
stack	B-General_Concept
frame	O
where	O
that	O
variable	O
is	O
declared	O
.	O
</s>
<s>
In	O
languages	O
with	O
closures	B-Language
,	O
variables	O
must	O
continue	O
to	O
exist	O
as	O
long	O
as	O
any	O
existing	O
closures	B-Language
have	O
references	O
to	O
them	O
.	O
</s>
<s>
This	O
is	O
most	O
commonly	O
implemented	O
using	O
some	O
form	O
of	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
A	O
closure	B-Language
can	O
be	O
used	O
to	O
associate	O
a	O
function	O
with	O
a	O
set	O
of	O
"	O
private	O
"	O
variables	O
,	O
which	O
persist	O
over	O
several	O
invocations	O
of	O
the	O
function	O
.	O
</s>
<s>
The	O
scope	B-Language
of	O
the	O
variable	O
encompasses	O
only	O
the	O
closed-over	O
function	O
,	O
so	O
it	O
cannot	O
be	O
accessed	O
from	O
other	O
program	O
code	O
.	O
</s>
<s>
These	O
are	O
analogous	O
to	O
private	O
variables	O
in	O
object-oriented	B-Language
programming	I-Language
,	O
and	O
in	O
fact	O
closures	B-Language
are	O
analogous	O
to	O
a	O
type	O
of	O
object	B-Language
,	O
specifically	O
function	B-Language
objects	I-Language
,	O
with	O
a	O
single	O
public	O
method	O
(	O
function	O
call	O
)	O
,	O
and	O
possibly	O
many	O
private	O
variables	O
(	O
the	O
closed-over	O
variables	O
)	O
.	O
</s>
<s>
In	O
stateful	O
languages	O
,	O
closures	B-Language
can	O
thus	O
be	O
used	O
to	O
implement	O
paradigms	O
for	O
state	O
representation	O
and	O
information	B-Application
hiding	I-Application
,	O
since	O
the	O
closure	B-Language
's	O
upvalues	B-Language
(	O
its	O
closed-over	O
variables	O
)	O
are	O
of	O
indefinite	O
extent	O
,	O
so	O
a	O
value	O
established	O
in	O
one	O
invocation	O
remains	O
available	O
in	O
the	O
next	O
.	O
</s>
<s>
Closures	B-Language
used	O
in	O
this	O
way	O
no	O
longer	O
have	O
referential	O
transparency	O
,	O
and	O
are	O
thus	O
no	O
longer	O
pure	B-Application
functions	I-Application
;	O
nevertheless	O
,	O
they	O
are	O
commonly	O
used	O
in	O
impure	O
functional	B-Language
languages	I-Language
such	O
as	O
Scheme	B-Language
.	O
</s>
<s>
Closures	B-Language
have	O
many	O
uses	O
:	O
</s>
<s>
Because	O
closures	B-Language
delay	O
evaluation	O
—	O
i.e.	O
,	O
they	O
do	O
not	O
"	O
do	O
"	O
anything	O
until	O
they	O
are	O
called	O
—	O
they	O
can	O
be	O
used	O
to	O
define	O
control	O
structures	O
.	O
</s>
<s>
For	O
example	O
,	O
all	O
of	O
Smalltalk	B-Application
's	O
standard	O
control	O
structures	O
,	O
including	O
branches	O
(	O
if/then/else	O
)	O
and	O
loops	O
(	O
while	O
and	O
for	O
)	O
,	O
are	O
defined	O
using	O
objects	O
whose	O
methods	B-Language
accept	O
closures	B-Language
.	O
</s>
<s>
In	O
Scheme	B-Language
:	O
</s>
<s>
Closures	B-Language
can	O
be	O
used	O
to	O
implement	O
object	B-Language
systems	I-Language
.	O
</s>
<s>
Note	O
:	O
Some	O
speakers	O
call	O
any	O
data	B-General_Concept
structure	I-General_Concept
that	O
binds	O
a	O
lexical	B-Language
environment	I-Language
a	O
closure	B-Language
,	O
but	O
the	O
term	O
usually	O
refers	O
specifically	O
to	O
functions	O
.	O
</s>
<s>
Closures	B-Language
are	O
typically	O
implemented	O
with	O
a	O
special	O
data	B-General_Concept
structure	I-General_Concept
that	O
contains	O
a	O
pointer	B-Language
to	I-Language
the	I-Language
function	I-Language
code	I-Language
,	O
plus	O
a	O
representation	O
of	O
the	O
function	O
's	O
lexical	B-Language
environment	I-Language
(	O
i.e.	O
,	O
the	O
set	O
of	O
available	O
variables	O
)	O
at	O
the	O
time	O
when	O
the	O
closure	B-Language
was	O
created	O
.	O
</s>
<s>
The	O
referencing	O
environment	O
binds	O
the	O
non-local	O
names	O
to	O
the	O
corresponding	O
variables	O
in	O
the	O
lexical	B-Language
environment	I-Language
at	O
the	O
time	O
the	O
closure	B-Language
is	O
created	O
,	O
additionally	O
extending	O
their	O
lifetime	O
to	O
at	O
least	O
as	O
long	O
as	O
the	O
lifetime	O
of	O
the	O
closure	B-Language
itself	O
.	O
</s>
<s>
When	O
the	O
closure	B-Language
is	O
entered	O
at	O
a	O
later	O
time	O
,	O
possibly	O
with	O
a	O
different	O
lexical	B-Language
environment	I-Language
,	O
the	O
function	O
is	O
executed	O
with	O
its	O
non-local	O
variables	O
referring	O
to	O
the	O
ones	O
captured	O
by	O
the	O
closure	B-Language
,	O
not	O
the	O
current	O
environment	O
.	O
</s>
<s>
A	O
language	O
implementation	O
cannot	O
easily	O
support	O
full	O
closures	B-Language
if	O
its	O
run-time	B-Library
memory	O
model	O
allocates	O
all	O
automatic	B-General_Concept
variables	I-General_Concept
on	O
a	O
linear	O
stack	B-General_Concept
.	O
</s>
<s>
However	O
,	O
a	O
closure	B-Language
requires	O
that	O
the	O
free	O
variables	O
it	O
references	O
survive	O
the	O
enclosing	O
function	O
's	O
execution	O
.	O
</s>
<s>
Therefore	O
,	O
those	O
variables	O
must	O
be	O
allocated	O
so	O
that	O
they	O
persist	O
until	O
no	O
longer	O
needed	O
,	O
typically	O
via	O
heap	B-Application
allocation	I-Application
,	O
rather	O
than	O
on	O
the	O
stack	B-General_Concept
,	O
and	O
their	O
lifetime	O
must	O
be	O
managed	O
so	O
they	O
survive	O
until	O
all	O
closures	B-Language
referencing	O
them	O
are	O
no	O
longer	O
in	O
use	O
.	O
</s>
<s>
This	O
explains	O
why	O
,	O
typically	O
,	O
languages	O
that	O
natively	O
support	O
closures	B-Language
also	O
use	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
The	O
alternatives	O
are	O
manual	B-Application
memory	I-Application
management	I-Application
of	O
non-local	O
variables	O
(	O
explicitly	O
allocating	O
on	O
the	O
heap	O
and	O
freeing	O
when	O
done	O
)	O
,	O
or	O
,	O
if	O
using	O
stack	B-General_Concept
allocation	I-General_Concept
,	O
for	O
the	O
language	O
to	O
accept	O
that	O
certain	O
use	O
cases	O
will	O
lead	O
to	O
undefined	B-Language
behaviour	I-Language
,	O
due	O
to	O
dangling	B-Error_Name
pointers	I-Error_Name
to	O
freed	O
automatic	B-General_Concept
variables	I-General_Concept
,	O
as	O
in	O
lambda	B-General_Concept
expressions	I-General_Concept
in	O
C++11	B-Language
or	O
nested	O
functions	O
in	O
GNU	B-Application
C	I-Application
.	O
The	O
funarg	B-Application
problem	I-Application
(	O
or	O
"	O
functional	O
argument	O
"	O
problem	O
)	O
describes	O
the	O
difficulty	O
of	O
implementing	O
functions	O
as	O
first	O
class	O
objects	O
in	O
a	O
stack-based	O
programming	O
language	O
such	O
as	O
C	B-Language
or	O
C++	B-Language
.	O
</s>
<s>
Similarly	O
in	O
D	B-Application
version	O
1	O
,	O
it	O
is	O
assumed	O
that	O
the	O
programmer	O
knows	O
what	O
to	O
do	O
with	O
delegates	O
and	O
automatic	O
local	O
variables	O
,	O
as	O
their	O
references	O
will	O
be	O
invalid	O
after	O
return	O
from	O
its	O
definition	O
scope	B-Language
(	O
automatic	O
local	O
variables	O
are	O
on	O
the	O
stack	B-General_Concept
)	O
–	O
this	O
still	O
permits	O
many	O
useful	O
functional	O
patterns	O
,	O
but	O
for	O
complex	O
cases	O
needs	O
explicit	O
heap	B-Application
allocation	I-Application
for	O
variables	O
.	O
</s>
<s>
D	B-Application
version	O
2	O
solved	O
this	O
by	O
detecting	O
which	O
variables	O
must	O
be	O
stored	O
on	O
the	O
heap	O
,	O
and	O
performs	O
automatic	O
allocation	O
.	O
</s>
<s>
Because	O
D	B-Application
uses	O
garbage	B-General_Concept
collection	I-General_Concept
,	O
in	O
both	O
versions	O
,	O
there	O
is	O
no	O
need	O
to	O
track	O
usage	O
of	O
variables	O
as	O
they	O
are	O
passed	O
.	O
</s>
<s>
In	O
strict	O
functional	B-Language
languages	I-Language
with	O
immutable	O
data	O
(	O
e.g.	O
</s>
<s>
Erlang	B-Operating_System
)	O
,	O
it	O
is	O
very	O
easy	O
to	O
implement	O
automatic	B-General_Concept
memory	I-General_Concept
management	I-General_Concept
(	O
garbage	B-General_Concept
collection	I-General_Concept
)	O
,	O
as	O
there	O
are	O
no	O
possible	O
cycles	O
in	O
variables	O
 '	O
references	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Erlang	B-Operating_System
,	O
all	O
arguments	O
and	O
variables	O
are	O
allocated	O
on	O
the	O
heap	O
,	O
but	O
references	O
to	O
them	O
are	O
additionally	O
stored	O
on	O
the	O
stack	B-General_Concept
.	O
</s>
<s>
Heap	O
cleaning	O
is	O
done	O
by	O
incremental	O
garbage	B-General_Concept
collector	I-General_Concept
.	O
</s>
<s>
In	O
ML	B-Language
,	O
local	O
variables	O
are	O
lexically	O
scoped	O
,	O
and	O
hence	O
define	O
a	O
stack-like	O
model	O
,	O
but	O
since	O
they	O
are	O
bound	O
to	O
values	O
and	O
not	O
to	O
objects	O
,	O
an	O
implementation	O
is	O
free	O
to	O
copy	O
these	O
values	O
into	O
the	O
closure	B-Language
's	O
data	B-General_Concept
structure	I-General_Concept
in	O
a	O
way	O
that	O
is	O
invisible	O
to	O
the	O
programmer	O
.	O
</s>
<s>
Scheme	B-Language
,	O
which	O
has	O
an	O
ALGOL-like	B-Language
lexical	O
scope	B-Language
system	O
with	O
dynamic	O
variables	O
and	O
garbage	B-General_Concept
collection	I-General_Concept
,	O
lacks	O
a	O
stack	B-General_Concept
programming	O
model	O
and	O
does	O
not	O
suffer	O
from	O
the	O
limitations	O
of	O
stack-based	O
languages	O
.	O
</s>
<s>
Closures	B-Language
are	O
expressed	O
naturally	O
in	O
Scheme	B-Language
.	O
</s>
<s>
The	O
lambda	B-Language
form	O
encloses	O
the	O
code	O
,	O
and	O
the	O
free	O
variables	O
of	O
its	O
environment	O
persist	O
within	O
the	O
program	O
as	O
long	O
as	O
they	O
can	O
possibly	O
be	O
accessed	O
,	O
and	O
so	O
they	O
can	O
be	O
used	O
as	O
freely	O
as	O
any	O
other	O
Scheme	B-Language
expression	O
.	O
</s>
<s>
Closures	B-Language
are	O
closely	O
related	O
to	O
Actors	O
in	O
the	O
Actor	B-Application
model	I-Application
of	O
concurrent	B-Architecture
computation	I-Architecture
where	O
the	O
values	O
in	O
the	O
function	O
's	O
lexical	B-Language
environment	I-Language
are	O
called	O
acquaintances	O
.	O
</s>
<s>
An	O
important	O
issue	O
for	O
closures	B-Language
in	O
concurrent	B-Architecture
programming	I-Architecture
languages	O
is	O
whether	O
the	O
variables	O
in	O
a	O
closure	B-Language
can	O
be	O
updated	O
and	O
,	O
if	O
so	O
,	O
how	O
these	O
updates	O
can	O
be	O
synchronized	O
.	O
</s>
<s>
Closures	B-Language
are	O
closely	O
related	O
to	O
function	B-Language
objects	I-Language
;	O
the	O
transformation	O
from	O
the	O
former	O
to	O
the	O
latter	O
is	O
known	O
as	O
defunctionalization	B-Application
or	O
lambda	B-Application
lifting	I-Application
;	O
see	O
also	O
closure	B-Application
conversion	I-Application
.	O
</s>
<s>
As	O
different	O
languages	O
do	O
not	O
always	O
have	O
a	O
common	O
definition	O
of	O
the	O
lexical	B-Language
environment	I-Language
,	O
their	O
definitions	O
of	O
closure	B-Language
may	O
vary	O
also	O
.	O
</s>
<s>
The	O
commonly	O
held	O
minimalist	O
definition	O
of	O
the	O
lexical	B-Language
environment	I-Language
defines	O
it	O
as	O
a	O
set	O
of	O
all	O
bindings	O
of	O
variables	O
in	O
the	O
scope	B-Language
,	O
and	O
that	O
is	O
also	O
what	O
closures	B-Language
in	O
any	O
language	O
have	O
to	O
capture	O
.	O
</s>
<s>
Although	O
the	O
relative	O
location	O
of	O
a	O
binding	O
does	O
not	O
change	O
at	B-Library
runtime	I-Library
,	O
the	O
value	O
in	O
the	O
bound	O
location	O
can	O
.	O
</s>
<s>
In	O
such	O
languages	O
,	O
since	O
closure	B-Language
captures	O
the	O
binding	O
,	O
any	O
operation	O
on	O
the	O
variable	O
,	O
whether	O
done	O
from	O
the	O
closure	B-Language
or	O
not	O
,	O
are	O
performed	O
on	O
the	O
same	O
relative	O
memory	O
location	O
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
illustrating	O
the	O
concept	O
in	O
ECMAScript	B-Language
,	O
which	O
is	O
one	O
such	O
language	O
:	O
</s>
<s>
Function	O
foo	O
and	O
the	O
closures	B-Language
referred	O
to	O
by	O
variables	O
f	O
and	O
g	O
all	O
use	O
the	O
same	O
relative	O
memory	O
location	O
signified	O
by	O
local	O
variable	O
x	O
.	O
</s>
<s>
In	O
some	O
instances	O
the	O
above	O
behaviour	O
may	O
be	O
undesirable	O
,	O
and	O
it	O
is	O
necessary	O
to	O
bind	O
a	O
different	O
lexical	B-Language
closure	I-Language
.	O
</s>
<s>
Again	O
in	O
ECMAScript	B-Language
,	O
this	O
would	O
be	O
done	O
using	O
the	O
Function.bind( )	O
.	O
</s>
<s>
For	O
this	O
example	O
the	O
expected	O
behaviour	O
would	O
be	O
that	O
each	O
link	O
should	O
emit	O
its	O
id	O
when	O
clicked	O
;	O
but	O
because	O
the	O
variable	O
'	O
e	O
 '	O
is	O
bound	O
the	O
scope	B-Language
above	O
,	O
and	O
lazy	O
evaluated	O
on	O
click	O
,	O
what	O
actually	O
happens	O
is	O
that	O
each	O
on	O
click	O
event	O
emits	O
the	O
id	O
of	O
the	O
last	O
element	O
in	O
'	O
elements	O
 '	O
bound	O
at	O
the	O
end	O
of	O
the	O
for	O
loop	O
.	O
</s>
<s>
Again	O
here	O
variable	O
e	O
would	O
need	O
to	O
be	O
bound	O
by	O
the	O
scope	B-Language
of	O
the	O
block	O
using	O
handle.bind(this )	O
or	O
the	O
let	O
keyword	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
many	O
functional	B-Language
languages	I-Language
,	O
such	O
as	O
ML	B-Language
,	O
bind	O
variables	O
directly	O
to	O
values	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
since	O
there	O
is	O
no	O
way	O
to	O
change	O
the	O
value	O
of	O
the	O
variable	O
once	O
it	O
is	O
bound	O
,	O
there	O
is	O
no	O
need	O
to	O
share	O
the	O
state	O
between	O
closures	B-Language
—	O
they	O
just	O
use	O
the	O
same	O
values	O
.	O
</s>
<s>
Java	B-Language
's	O
local	O
and	O
anonymous	O
classes	O
also	O
fall	O
into	O
this	O
category	O
—	O
they	O
require	O
captured	O
local	O
variables	O
to	O
be	O
final	O
,	O
which	O
also	O
means	O
there	O
is	O
no	O
need	O
to	O
share	O
state	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
C++11	B-Language
,	O
captured	O
variables	O
are	O
either	O
declared	O
with	O
[&],	O
which	O
means	O
captured	O
by	O
reference	O
,	O
or	O
with	O
[=],	O
which	O
means	O
captured	O
by	O
value	O
.	O
</s>
<s>
Yet	O
another	O
subset	O
,	O
lazy	O
functional	B-Language
languages	I-Language
such	O
as	O
Haskell	B-Language
,	O
bind	O
variables	O
to	O
results	O
of	O
future	O
computations	O
rather	O
than	O
values	O
.	O
</s>
<s>
Consider	O
this	O
example	O
in	O
Haskell	B-Language
:	O
</s>
<s>
The	O
binding	O
of	O
r	O
captured	O
by	O
the	O
closure	B-Language
defined	O
within	O
function	O
foo	O
is	O
to	O
the	O
computation	O
(	O
x	O
/	O
y	O
)	O
—	O
which	O
in	O
this	O
case	O
results	O
in	O
division	O
by	O
zero	O
.	O
</s>
<s>
However	O
,	O
since	O
it	O
is	O
the	O
computation	O
that	O
is	O
captured	O
,	O
and	O
not	O
the	O
value	O
,	O
the	O
error	O
only	O
manifests	O
itself	O
when	O
the	O
closure	B-Language
is	O
invoked	O
,	O
and	O
actually	O
attempts	O
to	O
use	O
the	O
captured	O
binding	O
.	O
</s>
<s>
In	O
some	O
languages	O
,	O
such	O
as	O
ECMAScript	B-Language
,	O
return	O
refers	O
to	O
the	O
continuation	O
established	O
by	O
the	O
closure	B-Language
lexically	O
innermost	O
with	O
respect	O
to	O
the	O
statement	O
—	O
thus	O
,	O
a	O
return	O
within	O
a	O
closure	B-Language
transfers	O
control	O
to	O
the	O
code	O
that	O
called	O
it	O
.	O
</s>
<s>
However	O
,	O
in	O
Smalltalk	B-Application
,	O
the	O
superficially	O
similar	O
operator	O
^	O
invokes	O
the	O
escape	O
continuation	O
established	O
for	O
the	O
method	O
invocation	O
,	O
ignoring	O
the	O
escape	O
continuations	O
of	O
any	O
intervening	O
nested	O
closures	B-Language
.	O
</s>
<s>
The	O
escape	O
continuation	O
of	O
a	O
particular	O
closure	B-Language
can	O
only	O
be	O
invoked	O
in	O
Smalltalk	B-Application
implicitly	O
by	O
reaching	O
the	O
end	O
of	O
the	O
closure	B-Language
's	O
code	O
.	O
</s>
<s>
These	O
examples	O
in	O
ECMAScript	B-Language
and	O
Smalltalk	B-Application
highlight	O
the	O
difference	O
:	O
</s>
<s>
The	O
above	O
code	O
snippets	O
will	O
behave	O
differently	O
because	O
the	O
Smalltalk	B-Application
^	O
operator	O
and	O
the	O
JavaScript	B-Language
return	O
operator	O
are	O
not	O
analogous	O
.	O
</s>
<s>
In	O
the	O
ECMAScript	B-Language
example	O
,	O
return	O
x	O
will	O
leave	O
the	O
inner	O
closure	B-Language
to	O
begin	O
a	O
new	O
iteration	O
of	O
the	O
forEach	O
loop	O
,	O
whereas	O
in	O
the	O
Smalltalk	B-Application
example	O
,	O
^x	O
will	O
abort	O
the	O
loop	O
and	O
return	O
from	O
the	O
method	O
foo	O
.	O
</s>
<s>
Common	B-Language
Lisp	I-Language
provides	O
a	O
construct	O
that	O
can	O
express	O
either	O
of	O
the	O
above	O
actions	O
:	O
Lisp	B-Language
(	O
return-from	O
foo	O
x	O
)	O
behaves	O
as	O
Smalltalk	B-Application
^x	O
,	O
while	O
Lisp	B-Language
(	O
return-from	O
nil	O
x	O
)	O
behaves	O
as	O
JavaScript	B-Language
return	O
x	O
.	O
</s>
<s>
Hence	O
,	O
Smalltalk	B-Application
makes	O
it	O
possible	O
for	O
a	O
captured	O
escape	O
continuation	O
to	O
outlive	O
the	O
extent	O
in	O
which	O
it	O
can	O
be	O
successfully	O
invoked	O
.	O
</s>
<s>
When	O
the	O
closure	B-Language
returned	O
by	O
the	O
method	O
foo	O
is	O
invoked	O
,	O
it	O
attempts	O
to	O
return	O
a	O
value	O
from	O
the	O
invocation	O
of	O
foo	O
that	O
created	O
the	O
closure	B-Language
.	O
</s>
<s>
Since	O
that	O
call	O
has	O
already	O
returned	O
and	O
the	O
Smalltalk	B-Application
method	O
invocation	O
model	O
does	O
not	O
follow	O
the	O
spaghetti	O
stack	B-General_Concept
discipline	O
to	O
facilitate	O
multiple	O
returns	O
,	O
this	O
operation	O
results	O
in	O
an	O
error	O
.	O
</s>
<s>
Some	O
languages	O
,	O
such	O
as	O
Ruby	B-Language
,	O
enable	O
the	O
programmer	O
to	O
choose	O
the	O
way	O
return	O
is	O
captured	O
.	O
</s>
<s>
An	O
example	O
in	O
Ruby	B-Language
:	O
</s>
<s>
Both	O
Proc.new	O
and	O
lambda	B-Language
in	O
this	O
example	O
are	O
ways	O
to	O
create	O
a	O
closure	B-Language
,	O
but	O
semantics	O
of	O
the	O
closures	B-Language
thus	O
created	O
are	O
different	O
with	O
respect	O
to	O
the	O
return	O
statement	O
.	O
</s>
<s>
In	O
Scheme	B-Language
,	O
definition	O
and	O
scope	B-Language
of	O
the	O
return	O
control	O
statement	O
is	O
explicit	O
(	O
and	O
only	O
arbitrarily	O
named	O
'	O
return	O
 '	O
for	O
the	O
sake	O
of	O
the	O
example	O
)	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
direct	O
translation	O
of	O
the	O
Ruby	B-Language
sample	O
.	O
</s>
<s>
Some	O
languages	O
have	O
features	O
which	O
simulate	O
the	O
behavior	O
of	O
closures	B-Language
.	O
</s>
<s>
In	O
languages	O
such	O
as	O
Java	B-Language
,	O
C++	B-Language
,	O
Objective-C	O
,	O
C#	B-Application
,	O
VB.NET	B-Language
,	O
and	O
D	B-Application
,	O
these	O
features	O
are	O
the	O
result	O
of	O
the	O
language	O
's	O
object-oriented	B-Language
paradigm	O
.	O
</s>
<s>
closures	B-Language
in	O
functionality	O
,	O
but	O
not	O
in	O
syntax	O
.	O
</s>
<s>
idiom	O
differs	O
from	O
type-safe	B-Language
closures	B-Language
in	O
C#	B-Application
,	O
Haskell	B-Language
or	O
ML	B-Language
.	O
</s>
<s>
With	O
a	O
GNU	B-Application
Compiler	I-Application
Collection	I-Application
(	O
GCC	B-Application
)	O
extension	O
,	O
a	O
can	O
be	O
used	O
and	O
a	O
function	B-Language
pointer	I-Language
can	O
emulate	O
closures	B-Language
,	O
providing	O
the	O
function	O
does	O
not	O
exit	O
the	O
containing	O
scope	B-Language
.	O
</s>
<s>
But	O
moving	O
adder	O
(	O
and	O
,	O
optionally	O
,	O
the	O
typedef	B-Language
)	O
in	O
main	O
makes	O
it	O
valid	O
:	O
</s>
<s>
Java	B-Language
enables	O
classes	O
to	O
be	O
defined	O
inside	O
methods	B-Language
.	O
</s>
<s>
A	O
local	B-Language
class	I-Language
(	O
either	O
named	O
or	O
anonymous	O
)	O
may	O
refer	O
to	O
names	O
in	O
lexically	O
enclosing	O
classes	O
,	O
or	O
read-only	O
variables	O
(	O
marked	O
as	O
final	O
)	O
in	O
the	O
lexically	O
enclosing	O
method	O
.	O
</s>
<s>
The	O
local	B-Language
class	I-Language
will	O
not	O
be	O
able	O
to	O
change	O
the	O
value	O
of	O
the	O
container	O
reference	O
itself	O
,	O
but	O
it	O
will	O
be	O
able	O
to	O
change	O
the	O
contents	O
of	O
the	O
container	O
.	O
</s>
<s>
With	O
the	O
advent	O
of	O
Java	B-Language
8	O
's	O
lambda	B-General_Concept
expressions	I-General_Concept
,	O
the	O
closure	B-Language
causes	O
the	O
above	O
code	O
to	O
be	O
executed	O
as	O
:	O
</s>
<s>
Local	O
classes	O
are	O
one	O
of	O
the	O
types	O
of	O
inner	B-Language
class	I-Language
that	O
are	O
declared	O
within	O
the	O
body	O
of	O
a	O
method	O
.	O
</s>
<s>
Java	B-Language
also	O
supports	O
inner	O
classes	O
that	O
are	O
declared	O
as	O
non-static	O
members	O
of	O
an	O
enclosing	O
class	O
.	O
</s>
<s>
Due	O
to	O
their	O
binding	O
to	O
these	O
instance	O
variables	O
,	O
an	O
inner	B-Language
class	I-Language
may	O
only	O
be	O
instantiated	O
with	O
an	O
explicit	O
binding	O
to	O
an	O
instance	O
of	O
the	O
enclosing	O
class	O
using	O
a	O
special	O
syntax	O
.	O
</s>
<s>
Beware	O
to	O
not	O
confuse	O
this	O
type	O
of	O
class	O
with	O
the	O
nested	B-Language
class	I-Language
,	O
which	O
is	O
declared	O
in	O
the	O
same	O
way	O
with	O
an	O
accompanied	O
usage	O
of	O
the	O
"	O
static	O
"	O
modifier	O
;	O
those	O
have	O
not	O
the	O
desired	O
effect	O
but	O
are	O
instead	O
just	O
classes	O
with	O
no	O
special	O
binding	O
defined	O
in	O
an	O
enclosing	O
class	O
.	O
</s>
<s>
As	O
of	O
Java	B-Language
8	O
,	O
Java	B-Language
supports	O
functions	O
as	O
first	O
class	O
objects	O
.	O
</s>
<s>
Lambda	B-General_Concept
expressions	I-General_Concept
of	O
this	O
form	O
are	O
considered	O
of	O
type	O
Functionxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
with	O
T	O
being	O
the	O
domain	O
and	O
U	O
the	O
image	O
type	O
.	O
</s>
<s>
The	O
expression	O
can	O
be	O
called	O
with	O
its	O
.apply(T t )	O
method	O
,	O
but	O
not	O
with	O
a	O
standard	O
method	B-Language
call	I-Language
.	O
</s>
<s>
Apple	O
introduced	O
blocks	B-Language
,	O
a	O
form	O
of	O
closure	B-Language
,	O
as	O
a	O
nonstandard	O
extension	O
into	O
C	B-Language
,	O
C++	B-Language
,	O
Objective-C	O
2.0	O
and	O
in	O
Mac	B-Device
OS	I-Device
X	I-Device
10.6	I-Device
"	I-Device
Snow	I-Device
Leopard	I-Device
"	I-Device
and	O
iOS	B-Operating_System
4.0	I-Operating_System
.	O
</s>
<s>
Apple	O
made	O
their	O
implementation	O
available	O
for	O
the	O
GCC	B-Application
and	O
clang	O
compilers	O
.	O
</s>
<s>
Blocks	B-Language
that	O
need	O
to	O
persist	O
outside	O
of	O
the	O
scope	B-Language
they	O
are	O
created	O
in	O
may	O
need	O
to	O
be	O
copied	O
.	O
</s>
<s>
C#	B-Application
anonymous	B-General_Concept
methods	I-General_Concept
and	O
lambda	B-General_Concept
expressions	I-General_Concept
support	O
closure	B-Language
:	O
</s>
<s>
Visual	B-Language
Basic	I-Language
.NET	I-Language
,	O
which	O
has	O
many	O
language	O
features	O
similar	O
to	O
those	O
of	O
C#	B-Application
,	O
also	O
supports	O
lambda	B-General_Concept
expressions	I-General_Concept
with	O
closures	B-Language
:	O
</s>
<s>
In	O
D	B-Application
,	O
closures	B-Language
are	O
implemented	O
by	O
delegates	O
,	O
a	O
function	B-Language
pointer	I-Language
paired	O
with	O
a	O
context	O
pointer	O
(	O
e.g.	O
</s>
<s>
a	O
class	O
instance	O
,	O
or	O
a	O
stack	B-General_Concept
frame	O
on	O
the	O
heap	O
in	O
the	O
case	O
of	O
closures	B-Language
)	O
.	O
</s>
<s>
D	B-Application
version	O
1	O
,	O
has	O
limited	O
closure	B-Language
support	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
above	O
code	O
will	O
not	O
work	O
correctly	O
,	O
because	O
the	O
variable	O
a	O
is	O
on	O
the	O
stack	B-General_Concept
,	O
and	O
after	O
returning	O
from	O
test( )	O
,	O
it	O
is	O
no	O
longer	O
valid	O
to	O
use	O
it	O
(	O
most	O
probably	O
calling	O
foo	O
via	O
dg( )	O
,	O
will	O
return	O
a	O
'	O
random	O
 '	O
integer	O
)	O
.	O
</s>
<s>
This	O
can	O
be	O
solved	O
by	O
explicitly	O
allocating	O
the	O
variable	O
'	O
a	O
 '	O
on	O
heap	O
,	O
or	O
using	O
structs	B-Application
or	O
class	O
to	O
store	O
all	O
needed	O
closed	O
variables	O
and	O
construct	O
a	O
delegate	O
from	O
a	O
method	O
implementing	O
the	O
same	O
code	O
.	O
</s>
<s>
Closures	B-Language
can	O
be	O
passed	O
to	O
other	O
functions	O
,	O
as	O
long	O
as	O
they	O
are	O
only	O
used	O
while	O
the	O
referenced	O
values	O
are	O
still	O
valid	O
(	O
for	O
example	O
calling	O
another	O
function	O
with	O
a	O
closure	B-Language
as	O
a	O
callback	O
parameter	O
)	O
,	O
and	O
are	O
useful	O
for	O
writing	O
generic	O
data	O
processing	O
code	O
,	O
so	O
this	O
limitation	O
,	O
in	O
practice	O
,	O
is	O
often	O
not	O
an	O
issue	O
.	O
</s>
<s>
This	O
limitation	O
was	O
fixed	O
in	O
D	B-Application
version	O
2	O
-	O
the	O
variable	O
'	O
a	O
 '	O
will	O
be	O
automatically	O
allocated	O
on	O
the	O
heap	O
because	O
it	O
is	O
used	O
in	O
the	O
inner	O
function	O
,	O
and	O
a	O
delegate	O
of	O
that	O
function	O
can	O
escape	O
the	O
current	O
scope	B-Language
(	O
via	O
assignment	O
to	O
dg	O
or	O
return	O
)	O
.	O
</s>
<s>
Any	O
other	O
local	O
variables	O
(	O
or	O
arguments	O
)	O
that	O
are	O
not	O
referenced	O
by	O
delegates	O
or	O
that	O
are	O
only	O
referenced	O
by	O
delegates	O
that	O
do	O
not	O
escape	O
the	O
current	O
scope	B-Language
,	O
remain	O
on	O
the	O
stack	B-General_Concept
,	O
which	O
is	O
simpler	O
and	O
faster	O
than	O
heap	B-Application
allocation	I-Application
.	O
</s>
<s>
The	O
same	O
is	O
true	O
for	O
inner	O
's	O
class	O
methods	B-Language
that	O
reference	O
a	O
function	O
's	O
variables	O
.	O
</s>
<s>
C++	B-Language
enables	O
defining	O
function	B-Language
objects	I-Language
by	O
overloading	O
operator( )	O
.	O
</s>
<s>
These	O
objects	O
behave	O
somewhat	O
like	O
functions	O
in	O
a	O
functional	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
They	O
may	O
be	O
created	O
at	B-Library
runtime	I-Library
and	O
may	O
contain	O
state	O
,	O
but	O
they	O
do	O
not	O
implicitly	O
capture	O
local	O
variables	O
as	O
closures	B-Language
do	O
.	O
</s>
<s>
As	O
of	O
the	B-Language
2011	I-Language
revision	I-Language
,	O
the	O
C++	B-Language
language	I-Language
also	O
supports	O
closures	B-Language
,	O
which	O
are	O
a	O
type	O
of	O
function	B-Language
object	I-Language
constructed	O
automatically	O
from	O
a	O
special	O
language	O
construct	O
called	O
lambda-expression	O
.	O
</s>
<s>
A	O
C++	B-Language
closure	B-Language
may	O
capture	O
its	O
context	O
either	O
by	O
storing	O
copies	O
of	O
the	O
accessed	O
variables	O
as	O
members	O
of	O
the	O
closure	B-Language
object	B-Language
or	O
by	O
reference	O
.	O
</s>
<s>
In	O
the	O
latter	O
case	O
,	O
if	O
the	O
closure	B-Language
object	B-Language
escapes	O
the	O
scope	B-Language
of	O
a	O
referenced	O
object	B-Language
,	O
invoking	O
its	O
operator( )	O
causes	O
undefined	B-Language
behavior	I-Language
since	O
C++	B-Language
closures	B-Language
do	O
not	O
extend	O
the	O
lifetime	O
of	O
their	O
context	O
.	O
</s>
<s>
Eiffel	B-Language
includes	O
inline	O
agents	O
defining	O
closures	B-Language
.	O
</s>
<s>
An	O
inline	O
agent	O
is	O
an	O
object	B-Language
representing	O
a	O
routine	O
,	O
defined	O
by	O
giving	O
the	O
code	O
of	O
the	O
routine	O
in-line	O
.	O
</s>
<s>
The	O
main	O
limitation	O
of	O
Eiffel	B-Language
agents	O
,	O
which	O
distinguishes	O
them	O
from	O
closures	B-Language
in	O
other	O
languages	O
,	O
is	O
that	O
they	O
cannot	O
reference	O
local	O
variables	O
from	O
the	O
enclosing	O
scope	B-Language
.	O
</s>
<s>
This	O
design	O
decision	O
helps	O
in	O
avoiding	O
ambiguity	O
when	O
talking	O
about	O
a	O
local	O
variable	O
value	O
in	O
a	O
closure	B-Language
-	O
should	O
it	O
be	O
the	O
latest	O
value	O
of	O
the	O
variable	O
or	O
the	O
value	O
captured	O
when	O
the	O
agent	O
is	O
created	O
?	O
</s>
<s>
Only	O
Current	O
(	O
a	O
reference	O
to	O
current	O
object	B-Language
,	O
analogous	O
to	O
this	O
in	O
Java	B-Language
)	O
,	O
its	O
features	O
,	O
and	O
arguments	O
of	O
the	O
agent	O
itself	O
can	O
be	O
accessed	O
from	O
within	O
the	O
agent	O
body	O
.	O
</s>
<s>
Embarcadero	O
C++Builder	O
provides	O
the	O
reserve	O
word	O
__closure	O
to	O
provide	O
a	O
pointer	O
to	O
a	O
method	O
with	O
a	O
similar	O
syntax	O
to	O
a	O
function	B-Language
pointer	I-Language
.	O
</s>
<s>
Standard	O
C	B-Language
allows	O
writing	O
a	O
for	O
a	O
pointer	O
to	O
a	O
function	O
type	O
using	O
the	O
following	O
syntax:In	O
a	O
similar	O
way	O
,	O
a	O
can	O
be	O
declared	O
for	O
a	O
pointer	O
to	O
a	O
method	O
using	O
this	O
syntax	O
:	O
</s>
