The Language Construct

A closure associates some code with some of the environment in which the closure was created. This sounds like an odd concept. Code is created by the compiler. Right?

The ancestor of a closure is a pointer to a function. In the lexically scoped C world, where the way in which variables are resolved in the function does not depend on the environment in which the function pointer is used, the idea of a closure is not compelling. If the language is dynamically scoped, it resolves names by looking up its call stack. In a dynamically scoped language, function pointers are tricky. When a pointer to function foo is passed from one function to another, the location of variables visible to foo may change. Sometimes you can do clever ...

Get Real-Time Java™ Platform Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.