The Artima Developer Community
Sponsored Link

Java Community News
Neal Gafter on a Definition of Closures

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Neal Gafter on a Definition of Closures Posted: Jan 29, 2007 12:36 PM
Reply to this message Reply
Summary
There are almost as many notions of what a closure is as there are proposals to add closures to Java. In a recent article-length blog post, Neal Gafter reviews the origin and history of closures, suggesting that developers need a better understanding of closures to properly evaluate the Java closure proposals.
Advertisement

Much attention has been directed lately to proposals to add closures to Java. According to Neal Gafter, an author of one such proposal, developers need to form a thorough understanding of closures when evaluating the merits of adding this feature to the Java language.

In an article-length blog post, A Definition of Closures, Gafter traces the origin of closures to Scheme, and the evolution of the closure idea in object-oriented languages, highlighting the significance of true closures in the languages that have them. Based on his definition, he also compares Java anonymous inner classes to closures, showing why anonymous classes fall short of the flexibility and clarity of true closures.

Noting the difference between Lisp's dynamic scoping and Scheme's lexical scoping rules, Gafter notes that:

Scheme was lexically scoped, like the lambda calculus and most mathematical notations, which means that a variable reference binds to the lexically enclosing definition for that name that was active at the time the enclosing lambda form was evaluated.

To explain the semantics in terms of the implementation, evaluating a lambda expression was said to produce a closure. This is a function value represented as an object that contains references to the current bindings for all the variables used inside the lambda expression but defined outside it. These are called the free variables. When this closure object, or function, is applied to arguments later, the variable bindings that had been captured in the closure are used to give meaning to the free variables appearing in the code. The term closure describes more than just the abstract language construct, it also describes its implementation.

This leads Gafter to define closure as follows:

A closure is a function that captures the bindings of free variables in its lexical context.

Gafter notes that Java does not current have a construct that meets that definition. In particular, anonymous classes don't:

Let's set aside the fact that local variables from enclosing scopes must be final to be used inside an anonymous class. The problem is that variable names are simply not resolved in the correct scope. They are resolved in the scope of the anonymous class that you're creating, not the enclosing scope. If you're creating an instance of an interface then it's probably not too much of a problem because most interfaces don't have any (constant) variable definitions. But anonymous inner classes fail every other [lexically scoped language construct], most of them fatally.

What do you think of Gafter's definition of closure? What do closures mean to you?

Topic: Threads Considered Harmful Previous Topic   Next Topic Topic: Parasoft Releases SOATest 5.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use