Clojure:编译时间插入前/后函数 [英] Clojure: Compile time insertion of pre/post functions

查看:123
本文介绍了Clojure:编译时间插入前/后函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Clojure:前置函数的后续跟踪



Goal



对于每个Clojure函数,我想要一个 pre >函数:

  • 右键执行




    • 函数返回之后



      现在,我想在我的* .clj文件中执行这个所有函数。







    $ b b

    问题:



    如何开始? Clojure Compiler的源代码应该是什么部分?



    谢谢!

    解决方案

    首先,这听起来像一个有点疯狂的事情。几乎肯定有更好的方法来实现任何合理的目标(即这是尖叫的 XY问题< a>)。但是,只要你说它只是一个学习练习,那就很好: - )



    我可以想到几个策略,你可能需要考虑在黑客编译器:




    • 创建自己的 defn 宏,创建。显然,你需要确保使用自己的版本的defn而不是内置的。

    • 在运行时(在加载后)运行命名空间,并将所有函数重新定义为相同函数的封装版本。可能会有点乱,但肯定会增强你对命名空间的理解: - )



    如果你真的想破解编译器,地点,可能只是通过黑客 defn core.clj


    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:

    • Create your own defn macro 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.
    • Walk your namespaces at runtime (after they are loaded) and redefine all functions to a wrapped version of the same function. Could get a bit messy but will certainly enhance your understanding of namespaces :-)

    If you really want to hack the compiler, the easiest place to make this change would probably be just by hacking defn in core.clj

    这篇关于Clojure:编译时间插入前/后函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆