Skip to content

Instantly share code, notes, and snippets.

@dgrnbrg
Last active December 12, 2015 09:29
Show Gist options
  • Save dgrnbrg/4751473 to your computer and use it in GitHub Desktop.
Save dgrnbrg/4751473 to your computer and use it in GitHub Desktop.
Let macro that attempts to add the name that the variable was declared with to the metadata
(defmacro let'
"This is a `let` macro that also includes metadata about
what name was used for each form if that form supports
metadata."
[bindings & body]
(let [bindings'
(mapcat
(fn [[name form]]
(let [tmp-name (gensym)
meta-form `(if (and
(instance? clojure.lang.IObj ~tmp-name)
(instance? clojure.lang.IMeta ~tmp-name))
(vary-meta assoc :let-name '~name)
~tmp-name)]
[tmp-name form name meta-form]))
(partition 2 bindings))]
(list* `let
(vec bindings')
body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment