Last active
April 11, 2017 14:09
-
-
Save Gonzih/5814945 to your computer and use it in GitHub Desktop.
Clojure try* macro experiment.
Part of small project that I'm working on right now (https://github.com/Gonzih/feeds2imap.clj).
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 try* | |
"Macro to catch multiple exceptions with one catch body. | |
Usage: | |
(try* | |
(println :a) | |
(println :b) | |
(catch* [A B] e (println (class e))) | |
(catch C e (println :C)) | |
(finally (println :finally-clause))) | |
Will be expanded to: | |
(try | |
(println :a) | |
(println :b) | |
(catch A e (println (class e))) | |
(catch B e (println (class e))) | |
(catch C e (println :C)) | |
(finally (println :finally-clause))) | |
" | |
[& body] | |
(letfn [(catch*? [form] | |
(and (seq form) | |
(= (first form) 'catch*))) | |
(expand [[_catch* classes & catch-tail]] | |
(map #(list* 'catch % catch-tail) classes)) | |
(transform [form] | |
(if (catch*? form) | |
(expand form) | |
[form]))] | |
(cons 'try (mapcat transform body)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment