Created
October 10, 2012 04:34
-
-
Save arohner/3863185 to your computer and use it in GitHub Desktop.
macro generating macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro def-with-disable-macro | |
"Generates a pair of macros, with-enable-foo and with-disable-foo, which use with-redefs to replace foo with (constantly nil), or restore it." | |
[v short-name] | |
(let [disable-name (symbol (format "with-disable-%s" short-name)) | |
enable-name (symbol (format "with-enable-%s" short-name))] | |
`(do | |
(let [v# #'~v | |
v-name# (quote ~v) | |
old-value# (deref #'~v) | |
ns# *ns*] | |
(defmacro ~disable-name [& body#] | |
`(with-redefs [~v-name# (constantly nil)] | |
(do | |
~@body#))) | |
(defmacro ~enable-name [& body#] | |
`(do | |
(with-redefs [~v-name# ~old-value#] | |
(do | |
~@body#)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(defmacro def-with-disable-macro
"Generates a pair of macros, with-enable-foo and with-disable-foo, which use with-redefs to replace foo with (constantly nil), or restore it."
[v short-name](let [disable-name %28symbol %28format "with-disable-%s" short-name))
enable-name (symbol (format "with-enable-%s" short-name))]
(do (let [v# #'~v v-name# (quote ~v) old-value# (deref #'~v) ns# *ns*] (defmacro ~disable-name [& body#]
(with-redefs [~v-name# (constantly nil)](do~@Body#)))
(defmacro ~enable-name [& body#]
`(do
(with-redefs [~v-name# ~old-value#](do
~@Body#))))))))