Common Lisp blocks provide a non-local exit mechanism very
throw, with lexical scoping.
This package actually implements
in terms of
catch; however, the lexical scoping allows the
byte-compiler to omit the costly
catch step if the
body of the block does not actually
cl-return-from the block.
The forms are evaluated as if by a
if any of the forms execute
they will jump out and return directly from the
cl-block returns the result of the last form unless
cl-return-from mechanism is quite similar to
throw mechanism. The main differences are
that block names are unevaluated symbols, rather than forms
(such as quoted symbols) that evaluate to a tag at run-time; and
also that blocks are always lexically scoped.
In a dynamically scoped
catch, functions called from the
catch body can also
throw to the
is not an option for
cl-return-from referring to a block name must appear
physically within the forms that make up the body of the block.
They may not appear within other called functions, although they may
appear within macro expansions or
lambdas in the body. Block
catch names form independent name-spaces.
In true Common Lisp,
the function or expander bodies with implicit blocks with the
same name as the function or macro. This does not occur in Emacs
Lisp, but this package provides
forms, which do create the implicit block.
The Common Lisp looping constructs defined by this package,
cl-dolist, also create implicit blocks
just as in Common Lisp.
Because they are implemented in terms of Emacs Lisp’s
throw, blocks have the same overhead as actual
catch constructs (roughly two function calls). However,
the byte compiler will optimize away the
if the block does
not in fact contain any
that jump to it. This means that
cl-do loops and
functions that don’t use
cl-return don’t pay the overhead to
This macro returns from the block named name, which must be
an (unevaluated) symbol. If a result form is specified, it
is evaluated to produce the result returned from the
nil is returned.
This macro is exactly like
(cl-return-from nil result).
Common Lisp loops like
cl-dolist implicitly enclose
This macro executes statements while allowing for control transfer to
user-defined labels. Each element of labels-or-statements can
be either a label (an integer or a symbol), or a cons-cell
(a statement). This distinction is made before macroexpansion.
Statements are executed in sequence, discarding any return value.
Any statement can transfer control at any time to the statements that follow
one of the labels with the special form
Labels have lexical scope and dynamic extent.