Skip to content

Instantly share code, notes, and snippets.

@danlentz
Forked from ifesdjeen/fn.clj
Created September 9, 2013 00:14
Show Gist options
  • Save danlentz/6489856 to your computer and use it in GitHub Desktop.
Save danlentz/6489856 to your computer and use it in GitHub Desktop.
;; Declare a function
(defn fun1 [] (println 1))
;; And a hash that would capture that function
(def h {:key fun1})
;; => {:key #<user$fun1 user$fun1@5e540696>}
;; Now, check out the value of the key
(:key h)
;; => #<user$fun1 user$fun1@5e540696>
;; Run the reference of function
((:key h))
;; That's println:
;; => 1
;; nil
;; Redefine the function:
(defn fun1 [] (println 2))
;; => #'user/fun1
;; Run it!
((:key h))
;; That's println:
;; => 1
;; nil
;; Compare references:
fun1
;; => #<user$fun1 user$fun1@3494e2d2>
(:key h)
;; => #<user$fun1 user$fun1@5e540696>
;; In order to avoid it, use #'
(def h {:key #'fun1})
;; That fixes everything. If you don't want to keep writing #' everywhere,
;; and you can afford abstracting things away, use macro:
(defmacro by-reference [a]
`(#'~a))
;; Of course I mean use #' within your own macro, no sense replacing pretty #' with `by-reference`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment