REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: closure

closure  spec  body

Defines a closure function.


spec [block!] - Help string (opt) followed by arg words (and opt type and string)

body [block!] - The body block of the function

See also:

closure?   does   func   function   has  


A closure is a special type of function that has persistent variables.

With a closure you can write a block inside the closure body and the block will remain persistent:

add2: closure [c d] [[c + d]]
do add2 1 2

This works because the variables of a closure remain valid, even outside the closure after it has been called. Such variables have indefinite extent. They are not limited to the lifetime of the function.

Note, however, that this luxury provided by closures is not without its costs. Closures require more time to evaluate as well as more memory space.

In essence a closure is an object. When you define the closure, it constructs a prototype object, and each time you call the closure, the prototype object is instantiated and the body code is evaluated within that context.

More about closures here.

More about the benefits and costs of closures here.

  TOC < Back Next > - WIP Wiki Feedback Admin