CSC/ECE 517 Fall 2011/ch1 1d sr: Difference between revisions
Line 9: | Line 9: | ||
A Closure is basically a function or a method (or a block, in the context of ruby) that has the following two properties - | A Closure is basically a function or a method (or a block, in the context of ruby) that has the following two properties - | ||
* One can pass it around like an object or as a value parameter to other functions/methods/blocks | |||
* It takes the snapshot and effectively "remembers" the values of all the variables that were in scope when the function was created and it is because of this property that it is able to access those variables when it is called even though they may no longer be in scope. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/] | |||
In order for a programming language to be able to support closures, it must support the notion of "first class functions." | In order for a programming language to be able to support closures, it must support the notion of "first class functions." |
Revision as of 16:23, 3 September 2011
Wiki Chapter: CSC/ECE 517 Fall 2011/ch1 1d sr
Introduction
Closures
What are Closures
Let us define what a closure as a programming construct means.
A Closure is basically a function or a method (or a block, in the context of ruby) that has the following two properties -
- One can pass it around like an object or as a value parameter to other functions/methods/blocks
- It takes the snapshot and effectively "remembers" the values of all the variables that were in scope when the function was created and it is because of this property that it is able to access those variables when it is called even though they may no longer be in scope. [1]
In order for a programming language to be able to support closures, it must support the notion of "first class functions." A first class function can be treated like an object in that you can pass it around as parameter or you can store it in collections. [2] Since a closure can be passed around as a value before calling it, it can be called in a completely different scope than the one in which it was created and it is because of this that it needs to retain the knowledge of the lexical environment in which it was defined. [3]
Let us articulate in detail what "saving a lexical environment of its definition" means when it comes to a closure. This is required because the ability of a language to provide "props" for saving lexical context of a function definition determines the level of difficulty and effectively feasibility of implementing closures in languages that do not support closures directly viz. the Statically Typed Languages like C, C++, Java. One way of doing this would be to make a copy of all the variables that are needed when a closure was defined. Alternatively, the lifetime of such variables can be extended not by explicitly copying them but by retaining a reference to them, thus making them not eligible for garbage collection. [4]
Why Closures
For Functional languages [5], whihc themselves are essentially stateless, closures offer a way to store some kind of state at least for as long as the closure lives. Closures help functional languages to be terse in expressing logic.
Closures in Dynamically Typed Languages
Example use of Closures in Dynamically Typed Langauges
Example in Ruby
Example in JS
Example in Python
Closures in Statically Typed Languages
The Problem
Limitations of Statically Typed Languages
Lexical Scope
Functions not as first class citizens of the language
Implementing Closures in Statically Typed Languages
C : function pointers
C++ : function objects
Java : anonymous inner classes
Closures and Static Scoping
(Explanation)
Case study of Scheme
References
1. Matz discussion on Closures