common lisp's return-from macro for clojure. Allows non-local exits from lexically scoped blocks.
Clojure Java
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


# clj-return-from

An implementation of common lisp's return-from for clojure

## Usage

The 'block' macro will establish a block with lexical scope. In this
lexical scope, a return-from-<blockname> macro can be used to
immediately exit the block with some return value.


(block foo
       (+ 1 2))

==> 3

(block foo
       (return-from-foo :bar)
       (+ 1 2))

==> :bar

Note: If you attempt to return-from a block on a different thread
than the one that established the block, you will get a runtime error.

I implemented clj-return-from for several reasons:

1) implementing non-local exits requires that you throw a custom
exception (that must at least hold the return value). That requires
implementing a named class that extends Throwable. That requires you
drop into Java. I've done that so you don't have to.

2) clj-return-from contains code to check that the block and the
return-from are on the same thread.

3) clj-return-from blocks have lexical scope. If you create a named block
that calls a function that happens to create a block with the same
name, they won't conflict.

4) clj-return-from is more concise than try / throw.

To install, add:

:dependencies [[clj-return-from "1.0.0"]]

to your project.clj file. The code is hosted on clojars.

## License

Copyright (C) 2010 Jeff Palmucci

Distributed under the Eclipse Public License, the same as Clojure.