Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm experimenting with a "new" kind of lisp macros.

Macros in lisp are just normal functions that receive the code they wrap as argument and return some modified code. Typically they will just wrap the passed code into some more code. But then there are code walking macro: macros that will traverse passed code to modify it in depth.

What I'm working on is "code diving" macros. Not only will they traverse the passed code, but they will resolve called functions and macros, fetch their source code and traverse it too. And so on. All the redefined fns/macros are accumulated in a let/macrolet binding, topologically sorted by call/dependency order. Instrumented code will call these local redefinitions, shadowing the global definition lexically.

This allow the programmer to write truly local monkey-patches for existing code he doesn't have control over (e.g, code from another library for instance). I'm writing this in Clojure, and the traditional way to do this is to temporarily change the global definitions of targeted variables using with-redefs. This is problematic because other threads will see these redefinitions, and not just threads created within the instrumented code, but already existing threads too.

Another way to do it is to just redefine the targeted functions globally, but then your modifications are available to the whole program for the rest of its execution.



THANK YOU! I've been trying to understand wtf lisp macros are for so long I'm embarrassed to say. I've heard that their "extremely powerful" and kept thinking "Macros? like in C? In Excel? WTF is the big deal about macros?"

Now I understand why I should learn lisp other than to play with its weird-but-kinda-cool-looking prefix notation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: