gpt4 book ai didi

clojure - 有没有办法在 lein repl 启动时将方法注入(inject)到 clojure.core 中?

转载 作者:行者123 更新时间:2023-12-02 12:44:35 25 4
gpt4 key购买 nike

:injections 关键字非常有用。但是,我希望在核心中动态安装几个函数以进行调试。如何做到这一点?

    :injections [(require 'spyscope.core)
(use '[cemerick.pomegranate :only (add-dependencies)])
(use '[clojure.tools.namespace.repl :only (refresh)])]

理想情况下,我希望 refresh 一直存在,这样我就可以在任何地方使用它

最佳答案

您可以使用 intern 来实现此目的,尽管我怀疑可能有更好的方法来始终提供调试功能。我曾将 internclojure.core 一起使用,有时我想用现有的函数来学习东西,但在其他命名空间中注入(inject)函数感觉太黑客了。

(intern 'clojure.core 'refresh (fn [] (println "refreshed!"))
(ns another-ns)
(refresh)
;=> refreshed!

在您的 project.clj 中,您可以使用 :repl-options关键,具体来说:init 。不过,这取决于您所考虑的工作流程,因为当 REPL 启动时,该函数不会在所有已存在的命名空间中可用,因为它们都已经引用了公共(public)变量在 clojure.core 中。

但是,您可以在 REPL 启动时调用 (clojure.tools.namespace.repl/refresh) 一次,以重新加载所有命名空间,然后该函数应该可以使用。我刚刚尝试了以下方法,它似乎有效:

:repl-options {:init (do (require 'clojure.tools.namespace.repl)
(intern 'clojure.core 'refresh clojure.tools.namespace.repl/refresh)
(clojure.tools.namespace.repl/refresh))}

关于clojure - 有没有办法在 lein repl 启动时将方法注入(inject)到 clojure.core 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com