patternMinor
What is the name for closure-type functions that use the call-site environment?
Viewed 0 times
thewhatenvironmenttypecallnameforthatsitefunctions
Problem
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:
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:
Is there a name for these type of functions? Are there languages that implement them? If not, why?
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?
Solution
You are talking about static vs. dynamic scoping of variables. Most langauges today have static scoping, but famously the early LISP had dynamic scoping.
Scoping is a general feature of a programming language and is not something that a closure does (although I suppose one could organize things so that it does). In any case, look up "dynamic" and "static" scoping.
Scoping is a general feature of a programming language and is not something that a closure does (although I suppose one could organize things so that it does). In any case, look up "dynamic" and "static" scoping.
Context
StackExchange Computer Science Q#60689, answer score: 5
Revisions (0)
No revisions yet.