CSC/ECE 517 Fall 2011/ch1 1d ss: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:


== Closures ==
== Closures ==
In the words of Matsumoto the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]


=== what exactly is Closure ===
=== what exactly is Closure ===

Revision as of 04:52, 8 September 2011

Closures in statically typed languages. Most languages that implement closures are dynamically typed. It is a challenge to implement closures in a statically typed language. Explain why, and cover attempts to mix the two. Consider also closures and static scoping, as in Scheme.

Introduction

We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.

statically vs dynamically typed

One simple way to differentiate between the two is:
In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.

Examples of statically typed : C,C++,Java,JADE,Pascal etc
Examples of dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.[1]

Closures

In the words of Matsumoto, the creator of Ruby language: A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. [2]

what exactly is Closure

Advantages of Closures

Implementation of Closures

In dynamically typed languages

statically typed languages, difficulty

In statically typed languages

Closures and Static Scoping

References