-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.clj
32 lines (26 loc) · 824 Bytes
/
macros.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
;; Copyright 2017-2018 Workiva Inc.
(ns humilia.macros
(:require [backtick :refer [resolve-symbol]]))
(defn- class-sym*
[sym]
(try
(-> sym resolve-symbol name Class/forName)
(catch Throwable e)))
(defn class-sym?
[sym]
(boolean (class-sym* sym)))
(defn sym->class
[sym]
(if-let [class (class-sym* sym)]
class
(throw (IllegalArgumentException. (format "Unable to resolve class-name: %s" (pr-str sym))))))
(defmacro when-class
"When the class can be found, this expands to the body; when not, it suppresses it."
[class & body]
(when (class-sym? class)
`(do ~@body)))
(defmacro if-class
"When the class can be found, this expands to the form in the `then` clause;
when not, it expands to the form in the `else` clause."
[class then else]
(if (class-sym? class) then else))