Home

Awesome

easy-macros: An easy way to write 90% of your macros

tdrhq

Easy-macros help you write macros of this form:

  (with-<something> (...args...)
     ...body...)

Under the hood, this automates the call-with pattern.

Examples

Let's rewrite some well known examples to show what we mean.

ignore-errors

First let's see how we might write ignore-errors the Old-Fashioned way:

(defmacro custom-ignore-errors (&body body)
  `(handler-case
     (progn ,@body)
    (error () nil)))

Not too bad, but it's error-prone. You might forget to use a ,, you might forget to wrap body in progn etc. But worst, if you change the definition of custom-ignore-errors, you will have to recompile all the functions that use it.

You can avoid some of these issues by using the CALL-WITH pattern:

(defmacro custom-ignore-errors (&body body)
  `(call-custom-ignore-errors (lambda () ,@body)))

(defun call-custom-ignore-errors (fn)
  (handler-case
    (funcall fn)
   (error () nil)))

Now most of the logic is inside a non-backticked function. But there's still some backquoting and macro expansion we need to do which is error-prone, and it's also very verbose for simple macros.

Use def-easy-macro to essentially automate this process:

(def-easy-macro custom-ignore-errors (&fn fn)
  (handler-case
     (funcall fn)
    (error () nil)))

This custom-ignore-errors has a slightly different API though:

(custom-ignore-errors ()
  ...body...)

All easy-macros takes a second list for arguments. This is true even if it takes no arguments and only a body.

Notice a few things:

We don't need to use funcall by the way, the following is equivalent:

(def-easy-macro custom-ignore-errors (&fn fn)
  (handler-case
     (fn)
    (error () nil)))

We're still figuring out which one we like better. This version obviously is lesser code, but it also breaks the expectation that arguments in the lambda-list are variables. But anyway, moving on to next examples.

with-open-file

(def-easy-macro with-custom-open-file (&binding stream file &rest args &fn fn)
  (let ((stream (apply #'open file args)))
    (unwind-protect
       (funcall fn stream)
      (close stream))))

This can be used almost exactly like with-open-file.

Notice a few things:

uiop:with-temporary-file

(def-easy-macro my-with-custom-temporary-file (&key &binding stream &binding pathname prefix suffix &fn)
   ;; ... you get the idea
    (funcall fn my-stream my-pathname))

I didn't build out the example completely, but I wanted to show you how you could write more complex arguments in the macro.

All the arguments named with &binding are not part of argument-list, they will be sequentially bound to the &fn body function. The rest of expressions form the lambda-list for the argument-list.

maplist

Common Lisp comes with dolist, but not a maplist. Let's implement a quick maplist macro using loop:

(def-easy-macro maplist (&binding x list &fn fn)
  (loop for value in list collect (funcall fn value))

Before def-easy-macro this would've been too much work to define for something simple. With def-easy-macro it's just as easy to work with as any regular function, so you tend to macrofy even tiny abstractions like this.

Caveats with redefinitions

Most redefinitions will automatically be applied to all callers. If you change the lambda-list (either &binding or otherwise), the new definition may not be compatible.

Installation

We're waiting on this to be part of the next Quicklisp distribution, in the meantime you can use quick-patch to install:

(ql:quickload :quick-patch)
(quick-patch:register "https://github.com/tdrhq/easy-macros.git" "main")
(quick-patch:checkout-all ".quick-patch/")

TODO

This library is NOT very polished.

However, even with its limited polish it's been ridiculously useful in my work, so I thought I should put it out there and accept feedback and pull requests. There are few things that I'd personally like to see:

(def-stuff my-stuff (...)
  ,@body)

Author

Arnold Noronha arnold@screenshotbot.io

License

Apache License, Version 2.0