4 ms·
This article considers "closure" to mean a type of internal representation of a function which references free bindings which would otherwise be stack-allocated
by adrusi 11y ago
This article considers "closure" to mean a type of internal representation of a function which references free bindings which would otherwise be stack-allocated.
This goes against the usage of "closure" that I've seen elsewhere, including the current wikipedia page for the topic[1]. Ususally I see "closure" used to denote the concept of a function that references free variables, not any specific implementation. Maybe within the guile codebase they have a construct called "closure" which implements closures in the most general way possible (which involves heap allocation no doubt).
But it's true: lambdas ≠ closures. "Lambda" refers to the syntax that a language uses to allow anonymous functions. A "closure" is a type of function that references free variables. Java 8, for example, has lambdas, but they are not full closures, because they can only reference local variables declared in higher scopes if those are declared as final.
Python, on the other hand, sort of has lambdas, but they are limited to a body consisting of a single expression, which doesn't allow e.g. conditionals. Even if it didn't support the lambda syntax, though, it would support closures, because it lets you declare a named function during a function call, then pass it elsewhere by referencing its name. These functions can reference free variables:
def mkincer():
n = 0
def inner():
nonlocal n
n += 1
return n
return inner
[1] https://en.wikipedia.org/wiki/Closure_(computer_programming) https://en.wikipedia.org/wiki/Closure_(computer_programming)
- sorokod 11y ago> if those are declared as final. Effectively final (a variable or parameter whose value is never changed after it is initialized)
- samth 11y agoNo, this isn't what the Wikipedia article says. To quote: In programming languages, closures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first- class functions. which is the same definition that wingo is using. Stack allocation is another technique for implementing lexical scope, if the relevant bindings are still on the stack.