This is a followup to Clojure: pre post functions
Goal
For every Clojure function, I want to have a pre and post function that gets executed:
- right before the function is evaluated and
-
right after the function returns
Now, I want to do this all functions in my *.clj files.
I would prefer (this is also an learning exercise) to do this at the Clojure Compiler level.
Question:
How do I get started on this? What part of the Clojure Compiler source code should I be reading? What documentation / tutorials on the internals of the Clojure Compiler I should be aware of?
Thanks!
First off, this sounds like a slightly crazy thing to do in general. There are almost certainly better ways to achieve any sensible objective (i.e. this is screaming “XY Problem“). But as long as you say it is just for a learning exercise, that is fine 🙂
I can think of a couple of strategies you might want to consider before hacking the compiler:
defnmacro that does the wrapping when functions are created. Obviously you’ll need to make sure your own version of defn is used rather than the built-in one. Probably the simplest solution.If you really want to hack the compiler, the easiest place to make this change would probably be just by hacking
defnin core.clj