Closures are functions that borrow the environment in which they are declared and can use and modify its variables at any times indipendently from where they're called, like in this example:
function createclosure(x)
return function() --Anonymous closure
print(x) --The variable x is caught and can be used anywhere
end
end
myclosure = createclosure(123)
myclosure() --prints "123"
What I am looking for is quite the opposite: functions that borrow the environment in which they are called and can use and modify its variables:
function anti_closure()
x = x + 10 --x is borrowed from the environment in which the function is called
print(x)
end
function environment(x)
print(x)
anti_closure()
x = x - 5
print(x)
end
environment(23) --prints "23", "33" (from the anticlosure) and then "28" (because x was permanently modified in the anticlosure)
Is there a name for these type of functions? Are there languages that implement them? If not, why?