Print
Closures

Introduction

Martin Fowler has a good introduction to closures on his bliki.

Syntax

There are two syntaxes for closures: a block based syntax with syntactically significant whitespace and a braces based that ignores whitespace.

Block based syntax

Braces based

Semantics

Boo closures have have full access (to read and write) to their enclosing lexical environment. For Instance:

The best source of information right now are the test cases for closures in the tests/testcases/integration directory.

Closures vs. Functions

See Functions As Objects.

Some things you can do with named functions that you cannot with closures include recursion and overloading:

This will not work because "c" is unknown from inside the closure:

so you can use a regular named function or else create a 2nd callable to hold the name:

And you can use regular named functions to overload a method:

Powered by Atlassian Confluence