idiomatic way of extending clojure reify
So far, this is the solution I've found but I believe it's not very idiomatic ...
any better suggestions to dynamically extend reify?
UPDATE!
My idea would be adding a debugging call to println before the execution of certains functions of a current reify implementation.On the example provided i add a short string to the current implementation. Other cases could be timming or commons aspects that you can find on AspectOrientedProgramming
Thanks!
(def r (let [f "foo"]
(reify Object
(toString [this]
f))))
(str r) ; == "foo"
(def r-extended (let [f "extended"]
(reify Object
(toString [this]
(str f "-"(str r))))))
(str r-extended) ; == "extended-foo"
As far as I can tell your goal is to wrap an object created by reify and inject new behavior on calls to that object.
One direction would be to write a macro that takes the place of reify and inserts an "around" call for every reified function to a function you provide. Use would maybe look like this:
(reifyw Object wrapf
(toString [this]
f)))
which would turn into:
(reify Object
(toString [this]
(wrapf f)))
Another direction would be to just wrap calls to the object's interface to insert your own dynamic behavior - that would be done at the point of use instead of the point of declaration which has tradeoffs.
链接地址: http://www.djcxy.com/p/66880.html上一篇: 在clojure中追踪以前的价值的惯用方法是什么?
下一篇: 习惯性地扩展clojure的方式