UP | HOME

Metabang-bind — Unified Binding for Common Lisp

Table of Contents

Definition

metabang-bind is a Common Lisp library that provides a single bind macro unifying many kinds of destructuring and binding: let, multiple-value-bind, destructuring-bind, CLOS slot access (with-slots / with-accessors), and more. It reduces boilerplate when binding values from structured objects.

Online documentation: common-lisp.net/project/metabang-bind

Forms Used in Tolk

bind — general binding

(bind ((x 1)
       (y 2))
  (+ x y))

Works like let* for simple bindings.

(:slots ...) — slot binding (replaces with-slots)

(bind (((:slots n) ast))
  n)

Binds the slot named n of ast to the local variable n. Equivalent to:

(with-slots (n) ast
  n)

(:accessors ...) — accessor binding (replaces with-accessors)

When a slot has a reader (accessor) method, bind can use it for efficiency:

(bind (((:accessors (my-val val-reader)) obj))
  my-val)

defmethod-bind uses (:accessors ...) for slots that have reader methods and (:slots ...) for slots without, choosing the most efficient option at macro-expansion time (via closer-mop, see Closer-mop).

bind-slots — convenience wrapper (tolk/utils)

Tolk defines bind-slots as a shorthand when all bindings are slot bindings:

;; instead of:
(bind (((:slots l r) ast)
       ((:slots n) (interpret l)))
  ...)

;; write:
(bind-slots (((l r) ast)
             ((n)   (interpret l)))
  ...)

Role in Defmethod-bind

defmethod-bind expands into a defmethod whose body opens with a bind call that destructures the method's specialised argument. See Defmethod-bind for the full expansion.

Loading

(ql:quickload :metabang-bind)

Related

  • Closer-mop — used by defmethod-bind to choose accessors vs. slots
  • Defmethod-bind — the macro built on top of metabang-bind
  • Tolk — uses metabang-bind throughout