Question

How it is that you can pass a var when a value is expected?

#clojure

Answer

(let [+ -]
  [(+ 1 2) (@#'+ 1 2)])
; => [-1 3]

In that last example the deref can even be left out:

(let [+ -]
  [(+ 1 2) (#'+ 1 2)])
; => [-1 3]

This is because Var implements IFn (the interface for Clojure functions) by calling deref on itself, casting the result to IFn and delegating the function call to that.

[^1]: http://stackoverflow.com/questions/9113387/difference-between-symbols-and-vars-in-clojure
[^2]: https://stackoverflow.com/a/39550785

Learn

Author

xavi

Reviews
Last
next

Students 0