trivial-with
2017-08-30
Replace nested with-xxx invocations with a single with:all form
Upstream URL
Author
License
TRIVIAL-WITH
TRIVIAL-WITH
is a simple library designed to contain multiple levels of WITH-
invocations into a single WITH:-
form.
There are many Lisp idioms prefixed with WITH-
. By containing an environment (and often hiding the cleanup code) they make Lisp a great language. However, having a lot of nested WITH-
forms is a visual nuisance.
with:all
is implemented as a macro that expands into nested WITH-xxx
forms. Because it expands syntactically, you may use it with any Lisp macro that starts with the characters "WITH-".
(with:all
((open-file (in "my-file"))
(slots (x y) rect)
(foreign-slots ...
...)
...
)
NOTES
WITH- forms that return a value
Most with- style macros create an environment in the contained body of code. There are exceptions such as with-output-to-string, which by default returns a string. Placed inside a with:all
macro the string would be lost (unless it (with-output-to-string..)
form was the first one in the list.
TRIVIAL-WITH
can capture the return value of a (WITH-xxx)
by setting the value of a symbol that precedes the form by using `(with:side-effect var ...)
(with:all
((open-file (in "my-file"))
(slots (x y) rect)
(side-effect z (output-to-string (x))
...
)
...
)
The above will basically expand to (setf z (with-output-to-string...))
The variable z of course must be in scope for this to work.