CSC/ECE 517 Fall 2011/ch1 1d gs

From Expertiza_Wiki
Revision as of 03:10, 8 September 2011 by Ssounda2 (talk | contribs)
Jump to navigation Jump to search

Closures for Statically Typed Languages

Introduction

This wiki gives an introduction to language constructs called closures, it’s usage and discusses about challenges involved in implementing them in statically typed languages.

Closures

A closure is a kind of routine that can be assigned to a variable or passed as a parameter to another routine. It can access the local state (local variables, parameters, methods, and instance variables) which is visible in the place it was defined.

To rephrase, a closure is a block of code which meets the following three criteria

1. It can be passed around as a value.

2. It can executed on demand by anyone who has that value, at which time.

3. It can refer to variables from the context in which it was created.

Why do we need closures and what are its uses?

DRY(Don't Repeat Yourself) is a popular software development principle, formulated by Andy Hunt and Dave Thomas, which stresses the importance of not duplicating code. Closures help in implementing the DRY principle and make the code easy to maintain.Closures increase considerably the level of a language by mixing access to local variables with remote execution of a set of locally-defined statements.

Lets see an example,

 def paidMore(amount)
 return Proc.new {|e| e.salary > amount}
 end

This function returns a closure, the behavior of which depends on the parameter amount passed to the enclosing function.


Amount can be fixed to a value like below, the variable highPaid contains a block of code (called a Proc in Ruby) that will return whether an employee’s salary is greater than 150.

 highPaid = paidMore(150)
 //Let’s check john’s salary
 john = Employee.new
 john.salary = 200
 print highPaid.call(john)

The expression highPaid.call(john) executes the e.salary > 150 block and prints the result of the execution. As long as a closure lives, all the free variables accessed by it are not eligible for garbage collection. So, the variable amount persists until the closure returned by paidMore does. Hence from the above sample code , it is clear that even if value 150 went out of scope, at the time of issuing the print call , the binding still remains.


The art of getting the best possible results by minimal code and the ease with which a user can use closures makes the latter a big success in Ruby.


A function to determine if an employee is a manager.Using C#, I'd probably write it like this.

public static IList Managers(IList emps) {
   IList result = new ArrayList();
   foreach(Employee e in emps)
     if (e.IsManager) result.Add(e);
   return result;
 }


In a language that has Closures, in this case Ruby, I'd write this.

 def managers(emps)
   return emps.select {|e| e.isManager}
 end

Statically Typed vs Dynamically Typed Languages

A programming language is said to use static typing when type checking is performed during compile-time as opposed to the run-time. Statically typed languages include Java , Objective-C ,Pascal etc.

A dynamically typed language is when the majority of its type checking is performed at run-time as opposed to that of compile-time. In dynamic typing values have types, but variables do not (a variable can refer to a value of any type) . Dynamically typed languages include Ruby,Smalltalk , Python ,JavaScript etc.

An important fact to note here is that the presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java and some other ostensibly statically typed languages, support downcasting and other type operations that depend on runtime type checks, a form of dynamic typing.

Closures in Dynamically Typed Languages

Closures in Ruby

Scheme Closures and Scoping

Closures in Statically Typed Languages[A Challenge, Why?]

Closure like constructs in C

Closures in Java

Java Lexical Scoping for Closures

Return from Closure

Safety

References