Suppose I've got a bunch of namespaces (apple, banana, orange). In these namespaces I use the eat
macro, which calls (not "generates", calls) the peel
function. The peel
function is different for each fruit, but the macros are identical, and fairly big, so I'd like to create a fruit
namespace that contains the eat
macro. But when I call the eat
macro from the apple
namespace, the eat
macro should call the apple/peel
function.
To illustrate (but this doesn't work):
(ns fruit)
(defmacro eat [] (peel))
(ns apple)
(defn peel [] (prn "peeled apple"))
(fruit/eat)
(ns banana)
(defn peel [] (prn "peeled banana"))
(fruit/eat)
To emphasize, this means that the peel function should be called when, and only when, the macro is expanded, as in this example.
(ns apple)
(defn peel [] (prn "peeled apple"))
(defmacro eat [] (peel))
(macroexpand-1 '(eat))
So, any ideas on how to combine macros and polymorphism?