Skip to content

Instantly share code, notes, and snippets.

@mrrodriguez
Last active December 31, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrodriguez/8046814 to your computer and use it in GitHub Desktop.
Save mrrodriguez/8046814 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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