Last active
August 29, 2015 14:10
-
-
Save patrickgombert/278b9ea067ebf772683a to your computer and use it in GitHub Desktop.
allow def'd classes to be used in try...catch
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
; try is a special form that seems to not allow def'd classes to be used | |
; for example: | |
; | |
; (def illegal-argument IllegalArgumentException) | |
; | |
; (try (catch illegal-argument _)) | |
; | |
; will raise the following exception: | |
; Unable to resolve classname: illegal-argument | |
(defmacro platform-try [& body] | |
(let [c (reduce (fn [acc item] | |
(if (and (seq? item) | |
(= 'platform-catch (first item))) | |
(rest item) | |
acc)) | |
nil body) | |
exception-class (eval (first c)) | |
exception-var (first (rest c)) | |
catch-block (rest (rest c)) | |
try-block (take-while (fn [item] | |
(if (seq? item) | |
(not= (first item) 'platform-catch) | |
true)) body)] | |
`(try ~@try-block | |
(catch ~exception-class ~exception-var ~@catch-block)))) | |
(playform-try (throw (IllegalArgumentException.)) (platform-catch illegal-argument _ nil)) ; => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment