3

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)
2
  • You don't specify the relation you want between the macro app and the form (+ 2 2). Please clarify exactly what it is you're trying to do. Commented May 20, 2016 at 18:30
  • I would like the macro app to change the behavior of every function application in the namespace. Including (+ 2 2) and also (+ 1 2 3) and (f x) for any function f. Commented May 21, 2016 at 12:36

1 Answer 1

2

+ 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.

Sign up to request clarification or add additional context in comments.

6 Comments

That's great, but can I override application?
Do you mean something other than the effect of calling a function when you say application here? Perhaps an example with the desired output would help me understand
Are you asking if you can alter the behavior of all function calls everywhere?
Yes. I am asking if I can alter the behavior of all function calls in a namespace. Example use: executing a print statement every time a function is called when the code runs.
I added this to my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.