Last active
December 31, 2015 21:29
-
-
Save mrrodriguez/8046814 to your computer and use it in GitHub Desktop.
Macro returning fn objects with closures
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
;;; case 1 | |
(defn get-map-of-fn [x] (fn [] x)) | |
;= #'user/get-map-of-fn | |
(defmacro map-of-fn [arg] (get-map-of-fn arg )) | |
;= #'user/map-of-fn | |
(map-of-fn 1) | |
;= #<user$get_map_of_fn$fn__9648 user$get_map_of_fn$fn__9648@47020d4> | |
((map-of-fn 1)) | |
;= IllegalArgumentException No matching ctor found for class user$get_map_of_fn$fn__9648 clojure.lang.Reflector.invokeConstructor (Reflector.java:163) | |
;;; case 2 | |
(defn get-map-of-fn [x] {:f (fn [] x)}) | |
;= #'user/get-map-of-fn | |
(defmacro map-of-fn [arg] (get-map-of-fn arg )) | |
;= #'user/map-of-fn | |
(map-of-fn 1) | |
;= IllegalArgumentException No matching ctor found for class user$get_map_of_fn$fn__10283 clojure.lang.Reflector.invokeConstructor (Reflector.java:163) |
;;; For case 2
(macroexpand-1 '(map-of-fn 1))
;= {:f #<user$get_map_of_fn$fn__10283 user$get_map_of_fn$fn__10283@7321803a>}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Somewhat related:
http://stackoverflow.com/questions/11191992/functions-with-closures-and-eval-in-clojure
https://gist.github.com/hiredman/921874
https://groups.google.com/forum/#!topic/clojure-dev/9JB-vKjvbhE