Ben Greenman
Ben Greenman

Reputation: 2002

clojure: override function application

Is it possible to override the behavior of function application in Clojure?

For instance I'd like to make a namespace where:

(+ 2 2)

Evaluates to the constant 5 via a macro like:

(defmacro app [& args] 5)

Upvotes: 3

Views: 381

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

+ is just a function so you can, for your namespace, exclude referring to clojure.core/+ and then define your own addition function in that namespace.

Clojure does not offer a general hook for the concept of function application in this namespace. What the development tools do is read the namespace for all the symbols that contain functions, and then hook them with wrapping function that does the tracing. This only affects functions that exist in the namespace at the time this happens, so it's less general than what you are asking for.

see clojure.tools.trace and Robert Hooke for more examples of this.

Upvotes: 2

Related Questions