FANDOM


In functional programming, a closure is a encapsulated function that has access to an individual set of lexical scope variables.

Examples Edit

The following Lua example demonstrates a closure very well.

function x()
    local y = 0;
    return () y = y+1; return y; end
end

local v = x(); -- v now holds the anonymous function and is a closure

v(); --> 1
v(); --> 2

In this snippet, x returns a function. The function has access to all local variables in x, but the variables are a copy different from those directly in x.


16px-Pencil

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.