Created
October 28, 2018 21:19
-
-
Save zilti/f008287e05cefda634710361eea8860d to your computer and use it in GitHub Desktop.
Nested macro vs. macro plus function with eval
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
;; This approach works as intended: | |
(defn- connect-fn [instance iface method args & code] | |
(eval `(proxy [~iface] [] | |
(~(symbol (.getName method)) ~args | |
~@code)))) | |
(defmacro connect [instance method args & code] | |
`(let [functional-method# (first (clojure.lang.Reflector/getMethods (class ~instance) 1 ~(str "set" (camelcase (name method))) false)) | |
functional-para# (symbol (.getName (first (.getParameterTypes ^Method functional-method#))))] | |
(~(symbol (str ".set" (camelcase (name method)))) ~instance | |
(connect-fn ~instance functional-para# functional-method# '~args ~@code)))) | |
;; This here, however, does not: | |
(defmacro connect | |
"This macro is used to make use of functional interfaces." | |
[instance method args & code] | |
`(let [functional-method# (first (clojure.lang.Reflector/getMethods (class ~instance) 1 ~(str "set" (camelcase (name method))) false)) | |
functional-para# (symbol (.getName (first (.getParameterTypes ^Method functional-method#))))] | |
(~(symbol (str ".set" (camelcase (name method)))) ~instance | |
`(proxy [~functional-para#] [] | |
(~(symbol (.getName functional-method#)) ~'~args | |
~'~@code))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment