简体   繁体   English

扩展Clojure的惯用方式

[英]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! 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. 我的想法是在执行当前reify实现的某些功能之前向println添加调试调用。在提供的示例中,我向当前实现添加了一个短字符串。 Other cases could be timming or commons aspects that you can find on AspectOrientedProgramming 其他情况可能是在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. 据我所知,您的目标是包装一个由reify创建的对象,并在对该对象的调用上注入新的行为。

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. 一个方向是编写一个代替reify的宏,并为您提供的每个功能插入一个“ around”调用。 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. 另一个方向是只包装对对象接口的调用以插入您自己的动态行为-这将在使用时完成,而不是在声明时进行权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM