The special operator block allows grouping of several Lisp forms (like an implicit progn
) and it also takes a name to name the block. When the forms within the block are evaluated, the special operator return-from can be used to leave the block. For instance:
(block foo
(print 'hello) ; evaluated
(return-from foo)
(print 'goodbye)) ; not evaluated
;;=> NIL
return-from can also be provided with a return value:
(block foo
(print 'hello) ; evaluated
(return-from foo 42)
(print 'goodbye)) ; not evaluated
;;=> 42
Named blocks are useful when a chunk of code has a meaningful name, or when blocks are nested. In some context, only the ability to return from a block early is important. In that case, you can use nil as the block name, and return. Return is just like return-from, except that the block name is always nil.
Note: enclosed forms are not top-level forms. That's different from progn
, where the enclosed forms of a top-level progn
form are still considered top-level forms.