Last active
December 14, 2015 15:49
-
-
Save valpackett/5111105 to your computer and use it in GitHub Desktop.
Clojure macro for executing code if a library exists
This file contains hidden or 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 when-require [n & body] | |
(let [nn (eval n)] | |
(try (require nn) | |
(catch Throwable e nil)) | |
(when (find-ns nn) | |
`(do ~@body)))) | |
(defmacro if-require [n body1 body2] | |
(let [nn (eval n)] | |
(try (require nn) | |
(catch Throwable e nil)) | |
(if (find-ns nn) | |
`(do ~body1) | |
`(do ~body2)))) | |
; Usage example: | |
; | |
; (when-require 'immutant.messaging | |
; (immutant.messaging/start "topic.whatever") | |
; (immutant.messaging/publish "topic.whatever" "hi")) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment