Skip to content

Instantly share code, notes, and snippets.

@mrrodriguez
Last active Dec 31, 2015
Embed
What would you like to do?
Macro returning fn objects with closures
;;; 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)
@mrrodriguez

This comment has been minimized.

Copy link
Owner Author

@mrrodriguez mrrodriguez commented Dec 19, 2013

;;; 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