Closure (computer programming)

Last updated

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function [lower-alpha 1] together with an environment. [1] The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. [lower-alpha 2] Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

Contents

History and etymology

The concept of closures was developed in the 1960s for the mechanical evaluation of expressions in the λ-calculus and was first fully implemented in 1970 as a language feature in the PAL programming language to support lexically scoped first-class functions. [2]

Peter Landin defined the term closure in 1964 as having an environment part and a control part as used by his SECD machine for evaluating expressions. [3] Joel Moses credits Landin with introducing the term closure to refer to a lambda expression with open bindings (free variables) that have been closed by (or bound in) the lexical environment, resulting in a closed expression, or closure. [4] [5] This use was subsequently adopted by Sussman and Steele when they defined Scheme in 1975, [6] a lexically scoped variant of Lisp, and became widespread.

Sussman and Abelson also use the term closure in the 1980s with a second, unrelated meaning: the property of an operator that adds data to a data structure to also be able to add nested data structures. This use of the term comes from mathematics use, rather than the prior use in computer science. The authors consider this overlap in terminology to be "unfortunate." [7]

Anonymous functions

The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the lexical environment section below).

For example, in the following Python code:

deff(x):defg(y):returnx+yreturng# Return a closure.defh(x):returnlambday:x+y# Return a closure.# Assigning specific closures to variables.a=f(1)b=h(1)# Using the closures stored in variables.asserta(5)==6assertb(5)==6# Using closures without binding them to variables first.assertf(1)(5)==6# f(1) is the closure.asserth(1)(5)==6# h(1) is the closure.

the values of a and b are closures, in both cases produced by returning a nested function with a free variable from the enclosing function, so that the free variable binds to the value of parameter x of the enclosing function. The closures in a and b are functionally identical. The only difference in implementation is that in the first case we used a nested function with a name, g, while in the second case we used an anonymous nested function (using the Python keyword lambda for creating an anonymous function). The original name, if any, used in defining them is irrelevant.

A closure is a value like any other value. It does not need to be assigned to a variable and can instead be used directly, as shown in the last two lines of the example. This usage may be deemed an "anonymous closure".

The nested function definitions are not themselves closures: they have a free variable which is not yet bound. Only once the enclosing function is evaluated with a value for the parameter is the free variable of the nested function bound, creating a closure, which is then returned from the enclosing function.

Lastly, a closure is only distinct from a function with free variables when outside of the scope of the non-local variables, otherwise the defining environment and the execution environment coincide and there is nothing to distinguish these (static and dynamic binding cannot be distinguished because the names resolve to the same values). For example, in the below program, functions with a free variable x (bound to the non-local variable x with global scope) are executed in the same environment where x is defined, so it is immaterial whether these are actually closures:

x=1nums=[1,2,3]deff(y):returnx+ymap(f,nums)map(lambday:x+y,nums)

This is most often achieved by a function return, since the function must be defined within the scope of the non-local variables, in which case typically its own scope will be smaller.

This can also be achieved by variable shadowing (which reduces the scope of the non-local variable), though this is less common in practice, as it is less useful and shadowing is discouraged. In this example f can be seen to be a closure because x in the body of f is bound to the x in the global namespace, not the x local to g:

x=0deff(y):returnx+ydefg(z):x=1# local x shadows global xreturnf(z)g(1)# evaluates to 1, not 2

Applications

The use of closures is associated with languages where functions are first-class objects, in which functions can be returned as results from higher-order functions, or passed as arguments to other function calls; if functions with free variables are first-class, then returning one creates a closure. This includes functional programming languages such as Lisp and ML, and many modern, multi-paradigm languages, such as Julia, Python, and Rust. Closures are also often used with callbacks, particularly for event handlers, such as in JavaScript, where they are used for interactions with a dynamic web page.

Closures can also be used in a continuation-passing style to hide state. Constructs such as objects and control structures can thus be implemented with closures. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At run-time, when the outer function executes, a closure is formed, consisting of the inner function's code and references (the upvalues) to any variables of the outer function required by the closure.

First-class functions

Closures typically appear in languages with first-class functions—in other words, such languages enable functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers. For example, consider the following Scheme function:

; Return a list of all books with at least THRESHOLD copies sold.(define(best-selling-booksthreshold)(filter(lambda(book)(>=(book-salesbook)threshold))book-list))

In this example, the lambda expression (lambda (book) (>= (book-sales book) threshold)) appears within the function best-selling-books. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda expression and a reference to the threshold variable, which is a free variable inside the lambda expression.

The closure is then passed to the filter function, which calls it repeatedly to determine which books are to be added to the result list and which are to be discarded. Because the closure has a reference to threshold, it can use that variable each time filter calls it. The function filter might be defined in a separate file.

Here is the same example rewritten in JavaScript, another popular language with support for closures:

// Return a list of all books with at least 'threshold' copies sold.functionbestSellingBooks(threshold){returnbookList.filter(book=>book.sales>=threshold);}

The arrow operator => is used to define an arrow function expression, and an Array.filter method [8] instead of a global filter function, but otherwise the structure and the effect of the code are the same.

A function may create a closure and return it, as in this example:

// Return a function that approximates the derivative of f// using an interval of dx, which should be appropriately small.functionderivative(f,dx){returnx=>(f(x+dx)-f(x))/dx;}

Because the closure in this case outlives the execution of the function that creates it, the variables f and dx live on after the function derivative returns, even though execution has left their scope and they are no longer visible. In languages without closures, the lifetime of an automatic local variable coincides with the execution of the stack frame where that variable is declared. In languages with closures, variables must continue to exist as long as any existing closures have references to them. This is most commonly implemented using some form of garbage collection.

State representation

A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. These are analogous to private variables in object-oriented programming, and in fact closures are similar to stateful function objects (or functors) with a single call-operator method.

In stateful languages, closures can thus be used to implement paradigms for state representation and information hiding, since the closure's upvalues (its closed-over variables) are of indefinite extent, so a value established in one invocation remains available in the next. Closures used in this way no longer have referential transparency, and are thus no longer pure functions; nevertheless, they are commonly used in impure functional languages such as Scheme.

Other uses

Closures have many uses:

(definefoo#f)(definebar#f)(let((secret-message"none"))(set!foo(lambda(msg)(set!secret-messagemsg)))(set!bar(lambda()secret-message)))(display(bar)); prints "none"(newline)(foo"meet me by the docks at midnight")(display(bar)); prints "meet me by the docks at midnight"

Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions.

Implementation and theory

Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (i.e., the set of available variables) at the time when the closure was created. The referencing environment binds the non-local names to the corresponding variables in the lexical environment at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure. When the closure is entered at a later time, possibly with a different lexical environment, the function is executed with its non-local variables referring to the ones captured by the closure, not the current environment.

A language implementation cannot easily support full closures if its run-time memory model allocates all automatic variables on a linear stack. In such languages, a function's automatic local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore, those variables must be allocated so that they persist until no longer needed, typically via heap allocation, rather than on the stack, and their lifetime must be managed so they survive until all closures referencing them are no longer in use.

This explains why, typically, languages that natively support closures also use garbage collection. The alternatives are manual memory management of non-local variables (explicitly allocating on the heap and freeing when done), or, if using stack allocation, for the language to accept that certain use cases will lead to undefined behaviour, due to dangling pointers to freed automatic variables, as in lambda expressions in C++11 [10] or nested functions in GNU C. [11] The funarg problem (or "functional argument" problem) describes the difficulty of implementing functions as first class objects in a stack-based programming language such as C or C++. Similarly in D version 1, it is assumed that the programmer knows what to do with delegates and automatic local variables, as their references will be invalid after return from its definition scope (automatic local variables are on the stack) – this still permits many useful functional patterns, but for complex cases needs explicit heap allocation for variables. D version 2 solved this by detecting which variables must be stored on the heap, and performs automatic allocation. Because D uses garbage collection, in both versions, there is no need to track usage of variables as they are passed.

In strict functional languages with immutable data (e.g. Erlang), it is very easy to implement automatic memory management (garbage collection), as there are no possible cycles in variables' references. For example, in Erlang, all arguments and variables are allocated on the heap, but references to them are additionally stored on the stack. After a function returns, references are still valid. Heap cleaning is done by incremental garbage collector.

In ML, local variables are lexically scoped, and hence define a stack-like model, but since they are bound to values and not to objects, an implementation is free to copy these values into the closure's data structure in a way that is invisible to the programmer.

Scheme, which has an ALGOL-like lexical scope system with dynamic variables and garbage collection, lacks a stack programming model and does not suffer from the limitations of stack-based languages. Closures are expressed naturally in Scheme. The lambda form encloses the code, and the free variables of its environment persist within the program as long as they can possibly be accessed, and so they can be used as freely as any other Scheme expression.[ citation needed ]

Closures are closely related to Actors in the Actor model of concurrent computation where the values in the function's lexical environment are called acquaintances. An important issue for closures in concurrent programming languages is whether the variables in a closure can be updated and, if so, how these updates can be synchronized. Actors provide one solution. [12]

Closures are closely related to function objects; the transformation from the former to the latter is known as defunctionalization or lambda lifting; see also closure conversion.[ citation needed ]

Differences in semantics

Lexical environment

As different languages do not always have a common definition of the lexical environment, their definitions of closure may vary also. The commonly held minimalist definition of the lexical environment defines it as a set of all bindings of variables in the scope, and that is also what closures in any language have to capture. However the meaning of a variable binding also differs. In imperative languages, variables bind to relative locations in memory that can store values. Although the relative location of a binding does not change at runtime, the value in the bound location can. In such languages, since closure captures the binding, any operation on the variable, whether done from the closure or not, are performed on the same relative memory location. This is often called capturing the variable "by reference". Here is an example illustrating the concept in ECMAScript, which is one such language:

// Javascriptvarf,g;functionfoo(){varx;f=function(){return++x;};g=function(){return--x;};x=1;alert('inside foo, call to f(): '+f());}foo();// 2alert('call to g(): '+g());// 1 (--x)alert('call to g(): '+g());// 0 (--x)alert('call to f(): '+f());// 1 (++x)alert('call to f(): '+f());// 2 (++x)

Function foo and the closures referred to by variables f and g all use the same relative memory location signified by local variable x.

In some instances the above behaviour may be undesirable, and it is necessary to bind a different lexical closure. Again in ECMAScript, this would be done using the Function.bind().

Example 1: Reference to an unbound variable

[13]

varmodule={x:42,getX:function(){returnthis.x;}}varunboundGetX=module.getX;console.log(unboundGetX());// The function gets invoked at the global scope// emits undefined as 'x' is not specified in global scope.varboundGetX=unboundGetX.bind(module);// specify object module as the closureconsole.log(boundGetX());// emits 42

Example 2: Accidental reference to a bound variable

For this example the expected behaviour would be that each link should emit its id when clicked; but because the variable 'e' is bound to the scope above, and lazy evaluated on click, what actually happens is that each on click event emits the id of the last element in 'elements' bound at the end of the for loop. [14]

varelements=document.getElementsByTagName('a');// Incorrect: e is bound to the function containing the 'for' loop, not the closure of "handle"for(vareofelements){e.onclick=functionhandle(){alert(e.id);}}

Again here variable e would need to be bound by the scope of the block using handle.bind(this) or the let keyword.

On the other hand, many functional languages, such as ML, bind variables directly to values. In this case, since there is no way to change the value of the variable once it is bound, there is no need to share the state between closures—they just use the same values. This is often called capturing the variable "by value". Java's local and anonymous classes also fall into this category—they require captured local variables to be final, which also means there is no need to share state.

Some languages enable choosing between capturing the value of a variable or its location. For example, in C++11, captured variables are either declared with [&], which means captured by reference, or with [=], which means captured by value.

Yet another subset, lazy functional languages such as Haskell, bind variables to results of future computations rather than values. Consider this example in Haskell:

-- Haskellfoo::Fractionala=>a->a->(a->a)fooxy=(\z->z+r)wherer=x/yf::Fractionala=>a->af=foo10main=print(f123)

The binding of r captured by the closure defined within function foo is to the computation (x / y)—which in this case results in division by zero. However, since it is the computation that is captured, and not the value, the error only manifests when the closure is invoked, and then attempts to use the captured binding.

Closure leaving

Yet more differences manifest themselves in the behavior of other lexically scoped constructs, such as return, break and continue statements. Such constructs can, in general, be considered in terms of invoking an escape continuation established by an enclosing control statement (in case of break and continue, such interpretation requires looping constructs to be considered in terms of recursive function calls). In some languages, such as ECMAScript, return refers to the continuation established by the closure lexically innermost with respect to the statement—thus, a return within a closure transfers control to the code that called it. However, in Smalltalk, the superficially similar operator ^ invokes the escape continuation established for the method invocation, ignoring the escape continuations of any intervening nested closures. The escape continuation of a particular closure can only be invoked in Smalltalk implicitly by reaching the end of the closure's code. These examples in ECMAScript and Smalltalk highlight the difference:

"Smalltalk"foo| xs |xs:=#(1234).xsdo: [:x|^x].^0barTranscriptshow: (selffooprintString) "prints 1"
// ECMAScriptfunctionfoo(){varxs=[1,2,3,4];xs.forEach(function(x){returnx;});return0;}alert(foo());// prints 0

The above code snippets will behave differently because the Smalltalk ^ operator and the JavaScript return operator are not analogous. In the ECMAScript example, return x will leave the inner closure to begin a new iteration of the forEach loop, whereas in the Smalltalk example, ^x will abort the loop and return from the method foo.

Common Lisp provides a construct that can express either of the above actions: Lisp (return-from foo x) behaves as Smalltalk ^x, while Lisp (return-from nil x) behaves as JavaScript return x. Hence, Smalltalk makes it possible for a captured escape continuation to outlive the extent in which it can be successfully invoked. Consider:

"Smalltalk"foo^[ :x|^x ] bar|f|f:=selffoo.fvalue:123"error!"

When the closure returned by the method foo is invoked, it attempts to return a value from the invocation of foo that created the closure. Since that call has already returned and the Smalltalk method invocation model does not follow the spaghetti stack discipline to facilitate multiple returns, this operation results in an error.

Some languages, such as Ruby, enable the programmer to choose the way return is captured. An example in Ruby:

# Ruby# Closure using a Procdeffoof=Proc.new{return"return from foo from inside proc"}f.call# control leaves foo herereturn"return from foo"end# Closure using a lambdadefbarf=lambda{return"return from lambda"}f.call# control does not leave bar herereturn"return from bar"endputsfoo# prints "return from foo from inside proc"putsbar# prints "return from bar"

Both Proc.new and lambda in this example are ways to create a closure, but semantics of the closures thus created are different with respect to the return statement.

In Scheme, definition and scope of the return control statement is explicit (and only arbitrarily named 'return' for the sake of the example). The following is a direct translation of the Ruby sample.

; Scheme(definecall/cccall-with-current-continuation)(define(foo)(call/cc(lambda(return)(define(f)(return"return from foo from inside proc"))(f); control leaves foo here(return"return from foo"))))(define(bar)(call/cc(lambda(return)(define(f)(call/cc(lambda(return)(return"return from lambda"))))(f); control does not leave bar here(return"return from bar"))))(display(foo)); prints "return from foo from inside proc"(newline)(display(bar)); prints "return from bar"

Closure-like constructs

Some languages have features which simulate the behavior of closures. In languages such as C++, C#, D, Java, Objective-C, and Visual Basic (.NET) (VB.NET), these features are the result of the language's object-oriented paradigm.

Callbacks (C)

Some C libraries support callbacks. This is sometimes implemented by providing two values when registering the callback with the library: a function pointer and a separate void* pointer to arbitrary data of the user's choice. When the library executes the callback function, it passes along the data pointer. This enables the callback to maintain state and to refer to information captured at the time it was registered with the library. The idiom is similar to closures in functionality, but not in syntax. The void* pointer is not type safe so this C idiom differs from type-safe closures in C#, Haskell or ML.

Callbacks are used extensively in graphical user interface (GUI) widget toolkits to implement event-driven programming by associating general functions of graphical widgets (menus, buttons, check boxes, sliders, spinners, etc.) with application-specific functions implementing the specific desired behavior for the application.

Nested function and function pointer (C)

With a GNU Compiler Collection (GCC) extension, a nested function [15] can be used and a function pointer can emulate closures, provided the function does not exit the containing scope. The next example is invalid because adder is a top-level definition (depending on compiler version, it could produce a correct result if compiled with no optimizing, i.e., at -O0):

#include<stdio.h>typedefint(*fn_int_to_int)(int);// type of function int->intfn_int_to_intadder(intnumber){intadd(intvalue){returnvalue+number;}return&add;// & operator is optional here because the name of a function in C is a pointer pointing on itself}intmain(void){fn_int_to_intadd10=adder(10);printf("%d\n",add10(1));return0;}

But moving adder (and, optionally, the typedef) in main makes it valid:

#include<stdio.h>intmain(void){typedefint(*fn_int_to_int)(int);// type of function int->intfn_int_to_intadder(intnumber){intadd(intvalue){returnvalue+number;}returnadd;}fn_int_to_intadd10=adder(10);printf("%d\n",add10(1));return0;}

If executed this now prints 11 as expected.

Local classes and lambda functions (Java)

Java enables classes to be defined inside methods. These are called local classes. When such classes are not named, they are known as anonymous classes (or anonymous inner classes). A local class (either named or anonymous) may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.

classCalculationWindowextendsJFrame{privatevolatileintresult;// ...publicvoidcalculateInSeparateThread(finalURIuri){// The expression "new Runnable() { ... }" is an anonymous class implementing the 'Runnable' interface.newThread(newRunnable(){voidrun(){// It can read final local variables:calculate(uri);// It can access private fields of the enclosing class:result=result+10;}}).start();}}

The capturing of final variables enables capturing variables by value. Even if the variable to capture is non-final, it can always be copied to a temporary final variable just before the class.

Capturing of variables by reference can be emulated by using a final reference to a mutable container, for example, a one-element array. The local class will not be able to change the value of the container reference, but it will be able to change the contents of the container.

With the advent of Java 8's lambda expressions, [16] the closure causes the above code to be executed as:

classCalculationWindowextendsJFrame{privatevolatileintresult;// ...publicvoidcalculateInSeparateThread(finalURIuri){// The code () -> { /* code */ } is a closure.newThread(()->{calculate(uri);result=result+10;}).start();}}

Local classes are one of the types of inner class that are declared within the body of a method. Java also supports inner classes that are declared as non-static members of an enclosing class. [17] They are normally referred to just as "inner classes". [18] These are defined in the body of the enclosing class and have full access to instance variables of the enclosing class. Due to their binding to these instance variables, an inner class may only be instantiated with an explicit binding to an instance of the enclosing class using a special syntax. [19]

publicclassEnclosingClass{/* Define the inner class */publicclassInnerClass{publicintincrementAndReturnCounter(){returncounter++;}}privateintcounter;{counter=0;}publicintgetCounter(){returncounter;}publicstaticvoidmain(String[]args){EnclosingClassenclosingClassInstance=newEnclosingClass();/* Instantiate the inner class, with binding to the instance */EnclosingClass.InnerClassinnerClassInstance=enclosingClassInstance.newInnerClass();for(inti=enclosingClassInstance.getCounter();(i=innerClassInstance.incrementAndReturnCounter())<10;/* increment step omitted */){System.out.println(i);}}}

Upon execution, this will print the integers from 0 to 9. Beware to not confuse this type of class with the nested class, which is declared in the same way with an accompanied usage of the "static" modifier; those have not the desired effect but are instead just classes with no special binding defined in an enclosing class.

As of Java 8, Java supports functions as first class objects. Lambda expressions of this form are considered of type Function<T,U> with T being the domain and U the image type. The expression can be called with its .apply(T t) method, but not with a standard method call.

publicstaticvoidmain(String[]args){Function<String,Integer>length=s->s.length();System.out.println(length.apply("Hello, world!"));// Will print 13.}

Blocks (C, C++, Objective-C 2.0)

Apple introduced blocks, a form of closure, as a nonstandard extension into C, C++, Objective-C 2.0 and in Mac OS X 10.6 "Snow Leopard" and iOS 4.0. Apple made their implementation available for the GCC and clang compilers.

Pointers to block and block literals are marked with ^. Normal local variables are captured by value when the block is created, and are read-only inside the block. Variables to be captured by reference are marked with __block. Blocks that need to persist outside of the scope they are created in may need to be copied. [20] [21]

typedefint(^IntBlock)();IntBlockdownCounter(intstart){__blockinti=start;return[[^int(){returni--;}copy]autorelease];}IntBlockf=downCounter(5);NSLog(@"%d",f());NSLog(@"%d",f());NSLog(@"%d",f());

Delegates (C#, VB.NET, D)

C# anonymous methods and lambda expressions support closure:

vardata=new[]{1,2,3,4};varmultiplier=2;varresult=data.Select(x=>x*multiplier);

Visual Basic .NET, which has many language features similar to those of C#, also supports lambda expressions with closures:

Dimdata={1,2,3,4}Dimmultiplier=2Dimresult=data.Select(Function(x)x*multiplier)

In D, closures are implemented by delegates, a function pointer paired with a context pointer (e.g. a class instance, or a stack frame on the heap in the case of closures).

autotest1(){inta=7;returndelegate(){returna+3;};// anonymous delegate construction}autotest2(){inta=20;intfoo(){returna+5;}// inner functionreturn&foo;// other way to construct delegate}voidbar(){autodg=test1();dg();// =10   // ok, test1.a is in a closure and still existsdg=test2();dg();// =25   // ok, test2.a is in a closure and still exists}

D version 1, has limited closure support. For example, the above code will not work correctly, because the variable a is on the stack, and after returning from test(), it is no longer valid to use it (most probably calling foo via dg(), will return a 'random' integer). This can be solved by explicitly allocating the variable 'a' on heap, or using structs or class to store all needed closed variables and construct a delegate from a method implementing the same code. Closures can be passed to other functions, as long as they are only used while the referenced values are still valid (for example calling another function with a closure as a callback parameter), and are useful for writing generic data processing code, so this limitation, in practice, is often not an issue.

This limitation was fixed in D version 2 - the variable 'a' will be automatically allocated on the heap because it is used in the inner function, and a delegate of that function can escape the current scope (via assignment to dg or return). Any other local variables (or arguments) that are not referenced by delegates or that are only referenced by delegates that do not escape the current scope, remain on the stack, which is simpler and faster than heap allocation. The same is true for inner's class methods that reference a function's variables.

Function objects (C++)

C++ enables defining function objects by overloading operator(). These objects behave somewhat like functions in a functional programming language. They may be created at runtime and may contain state, but they do not implicitly capture local variables as closures do. As of the 2011 revision, the C++ language also supports closures, which are a type of function object constructed automatically from a special language construct called lambda-expression. A C++ closure may capture its context either by storing copies of the accessed variables as members of the closure object or by reference. In the latter case, if the closure object escapes the scope of a referenced object, invoking its operator() causes undefined behavior since C++ closures do not extend the lifetime of their context.

voidfoo(stringmyname){inty;vector<string>n;// ...autoi=std::find_if(n.begin(),n.end(),// this is the lambda expression:[&](conststring&s){returns!=myname&&s.size()>y;});// 'i' is now either 'n.end()' or points to the first string in 'n'// which is not equal to 'myname' and whose length is greater than 'y'}

Inline agents (Eiffel)

Eiffel includes inline agents defining closures. An inline agent is an object representing a routine, defined by giving the code of the routine in-line. For example, in

ok_button.click_event.subscribe(agent(x,y:INTEGER)domap.country_at_coordinates(x,y).displayend)

the argument to subscribe is an agent, representing a procedure with two arguments; the procedure finds the country at the corresponding coordinates and displays it. The whole agent is "subscribed" to the event type click_event for a certain button, so that whenever an instance of the event type occurs on that button – because a user has clicked the button – the procedure will be executed with the mouse coordinates being passed as arguments for x and y.

The main limitation of Eiffel agents, which distinguishes them from closures in other languages, is that they cannot reference local variables from the enclosing scope. This design decision helps in avoiding ambiguity when talking about a local variable value in a closure - should it be the latest value of the variable or the value captured when the agent is created? Only Current (a reference to current object, analogous to this in Java), its features, and arguments of the agent can be accessed from within the agent body. The values of the outer local variables can be passed by providing additional closed operands to the agent.

C++Builder __closure reserved word

Embarcadero C++Builder provides the reserved word __closure to provide a pointer to a method with a similar syntax to a function pointer. [22]

Standard C allows writing a typedef for a pointer to a function type using the following syntax:

typedefvoid(*TMyFunctionPointer)(void);

In a similar way, a typedef can be declared for a pointer to a method using this syntax:

typedefvoid(__closure*TMyMethodPointer)();

See also

Notes

  1. The function may be stored as a reference to a function, such as a function pointer.
  2. These names usually refer to values, mutable variables, or functions, but can also be other entities such as constants, types, classes, or labels.

Related Research Articles

<span class="mw-page-title-main">Common Lisp</span> Programming language standard

Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ANSI INCITS 226-1994 (S2018). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived from the ANSI Common Lisp standard.

<span class="mw-page-title-main">Scheme (programming language)</span> Dialect of Lisp

Scheme is a dialect of the Lisp family of programming languages. Scheme was created during the 1970s at the MIT Computer Science and Artificial Intelligence Laboratory and released by its developers, Guy L. Steele and Gerald Jay Sussman, via a series of memos now known as the Lambda Papers. It was the first dialect of Lisp to choose lexical scope and the first to require implementations to perform tail-call optimization, giving stronger support for functional programming and associated techniques such as recursive algorithms. It was also one of the first programming languages to support first-class continuations. It had a significant influence on the effort that led to the development of Common Lisp.

<span class="mw-page-title-main">Smalltalk</span> Object-oriented programming language released first in 1972

Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.

In computer programming, the scope of a name binding is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name.

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

In computer science, hygienic macros are macros whose expansion is guaranteed not to cause the accidental capture of identifiers. They are a feature of programming languages such as Scheme, Dylan, Rust, Nim, and Julia. The general problem of accidental capture was well known in the Lisp community before the introduction of hygienic macros. Macro writers would use language features that would generate unique identifiers or use obfuscated identifiers to avoid the problem. Hygienic macros are a programmatic solution to the capture problem that is integrated into the macro expander. The term "hygiene" was coined in Kohlbecker et al.'s 1986 paper that introduced hygienic macro expansion, inspired by terminology used in mathematics.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

In some programming languages, eval, short for the English evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval. The input to eval is not necessarily a string; it may be structured representation of code, such as an abstract syntax tree, or of special type such as code. The analog for a statement is exec, which executes a string as if it were a statement; in some languages, such as Python, both are present, while in other languages only one of either eval or exec is.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In computer science, the funarg problem(function argument problem) refers to the difficulty in implementing first-class functions in programming language implementations so as to use stack-based memory allocation of the functions.

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions as well. In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.

In computer programming, a nested function is a named function that is defined within another, enclosing, block and is lexically scoped within the enclosing block – meaning it is only callable by name within the body of the enclosing block and can use identifiers declared in outer blocks, including outer functions. The enclosing block is typically, but not always, another function.

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.

<span class="mw-page-title-main">Python syntax and semantics</span> Set of rules defining correctly structured programs

The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. It supports multiple programming paradigms, including structured, object-oriented programming, and functional programming, and boasts a dynamic type system and automatic memory management.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

Blocks are a non-standard extension added by Apple Inc. to Clang's implementations of the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+, although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+ and non-Apple systems.

In programming languages, name resolution is the resolution of the tokens within program expressions to the intended program components.

References

  1. Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." (Wikisource)
  2. Turner, David A. (2012). "Some History of Functional Programming Languages" (PDF). International Symposium on Trends in Functional Programming. Lecture Notes in Computer Science. Vol. 7829. Springer. pp. 1–20 See 12 §2, note 8 for the claim about M-expressions. doi:10.1007/978-3-642-40447-4_1. ISBN   978-3-642-40447-4.
  3. Landin, P.J. (January 1964). "The mechanical evaluation of expressions" (PDF). The Computer Journal. 6 (4): 308–320. doi:10.1093/comjnl/6.4.308.
  4. Moses, Joel (June 1970). "The Function of FUNCTION in LISP, or Why the FUNARG Problem Should Be Called the Environment Problem". ACM Sigsam Bulletin (15): 13–27. doi:10.1145/1093410.1093411. hdl:1721.1/5854. S2CID   17514262. AI Memo 199. A useful metaphor for the difference between FUNCTION and QUOTE in LISP is to think of QUOTE as a porous or an open covering of the function since free variables escape to the current environment. FUNCTION acts as a closed or nonporous covering (hence the term "closure" used by Landin). Thus we talk of "open" Lambda expressions (functions in LISP are usually Lambda expressions) and "closed" Lambda expressions. [...] My interest in the environment problem began while Landin, who had a deep understanding of the problem, visited MIT during 1966–67. I then realized the correspondence between the FUNARG lists which are the results of the evaluation of "closed" Lambda expressions in LISP and ISWIM's Lambda Closures.
  5. Wikström, Åke (1987). Functional Programming using Standard ML. Prentice Hall. ISBN   0-13-331968-7. The reason it is called a "closure" is that an expression containing free variables is called an "open" expression, and by associating to it the bindings of its free variables, you close it.
  6. Sussman, Gerald Jay; Steele, Guy L. Jr. (December 1975). Scheme: An Interpreter for the Extended Lambda Calculus (Report). AI Memo 349.
  7. Abelson, Harold; Sussman, Gerald Jay; Sussman, Julie (1996). Structure and Interpretation of Computer Programs. MIT Press. pp. 98–99. ISBN   0-262-51087-1.
  8. "array.filter". Mozilla Developer Center. 10 January 2010. Retrieved 9 February 2010.
  9. "Re: FP, OO and relations. Does anyone trump the others?". 29 December 1999. Archived from the original on 26 December 2008. Retrieved 23 December 2008.
  10. Lambda Expressions and Closures C++ Standards Committee. 29 February 2008.
  11. "6.4 Nested Functions". GCC Manual. If you try to call the nested function through its address after the containing function exits, all hell breaks loose. If you try to call it after a containing scope level exits, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.
  12. Foundations of Actor Semantics Will Clinger. MIT Mathematics Doctoral Dissertation. June 1981.
  13. "Function.prototype.bind()". MDN Web Docs. Retrieved 20 November 2018.
  14. "Closures". MDN Web Docs. Retrieved 20 November 2018.
  15. "Nested functions".
  16. "Lambda Expressions". The Java Tutorials.
  17. "Nested, Inner, Member, and Top-Level Classes". Joseph D. Darcy's Oracle Weblog. July 2007. Archived from the original on 31 August 2016.
  18. "Inner Class Example". The Java Tutorials: Learning the Java Language: Classes and Objects.
  19. "Nested Classes". The Java Tutorials: Learning the Java Language: Classes and Objects.
  20. "Blocks Programming Topics". Apple Inc. 8 March 2011. Retrieved 8 March 2011.
  21. Bengtsson, Joachim (7 July 2010). "Programming with C Blocks on Apple Devices". Archived from the original on 25 October 2010. Retrieved 18 September 2010.
  22. Full documentation can be found at http://docwiki.embarcadero.com/RADStudio/Rio/en/Closure