在 emacs 中重新加载 Clojure 文件
我刚刚开始学习 Clojure 和 Emacs。我已经运行了 Windows 版 Clojure Box,我希望能够在缓冲区中编写代码,然后在 REPL 中运行它,而不必一直调用
(use 'example.code)
。我知道 Cc Ck 但它不会重新加载命名空间。如果我用来
(in-ns 'example.code)
更改 repl 中的命名空间,它就可以工作。这样做的正确方法是什么?
I am just starting out learning Clojure and Emacs. I have got Clojure Box for windows running and I would like to be able to write code in a buffer then run it in the REPL without haveing to call
(use 'example.code)
all the time. I know about C-c C-k but it doesn't reload the namespace. If i use
(in-ns 'example.code)
to change namespace in the repl it works. What is the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
in-ns
是正确的方法之一。对我来说最“正确”的方式是
(require '[example.code :as ec])
并在 REPL 的user
命名空间中工作;这样我的一次性实验状态就保留在user
中,并且ec/foo
对我来说足够方便(并且它使foo
应该在哪里变得很明显来自)。您始终可以说(require :reload-all 'example.code)
(与use
相同)来强制重新编译。另外,这里有一个函数,可以使用
use
删除(从当前命名空间)从给定命名空间拉入的所有映射:最重要的是,您可以构建
并说出
(reuse 'example.code)
让您的命名空间接近全新的开始。 (请注意,1.2 的新功能如deftype
和defrecord
引入了一些复杂性......特别是,unuse
对没有影响import
ed 类——这包括记录和deftype
创建的类型:reload-all
仍然会导致deftype
等。要重新编译的表单,但我记得遇到了奇怪的情况,这似乎还不够......可能是我的错误,可能是我还没有完全探索这些功能的一些神秘方面。)in-ns
is one of the right ways.The way which feels most "right" to me is to
(require '[example.code :as ec])
and work in theuser
namespace at the REPL; that way my throwaway experimental state stays inuser
andec/foo
is convenient enough to me (and it makes it obvious wherefoo
is supposed to come from). You can always say(require :reload-all 'example.code)
(same works withuse
) to force recompilation.Also, here's a function to remove (from the current namespace) all mappings pulled in from a given namespace with
use
:On top of that you could build
and say
(reuse 'example.code)
to get something close to a fresh start with your namespace. (Note that 1.2 new features such asdeftype
&defrecord
introduce some complexities... In particular,unuse
has no effect onimport
ed class -- this includes records anddeftype
-created types.:reload-all
still causes thedeftype
et al. forms to be recompiled, but I remember hitting weird cases where this didn't seem to be enough... Possibly my error, possibly some arcane aspect of these features I haven't yet fully explored.)也许“肮脏”的方式只是简单地使用 (ns 'example.code) 切换到 repl 中缓冲区的名称空间。肮脏的原因是你将缓冲区定义与 repl 混合在一起。我认为 Marczyk 的答案是正确的。
Maybe the "dirty" way is simply switch to the namespace of the buffer in the repl with (ns 'example.code) . Dirty cause you mix the buffer definitions with repl ones. I think the Marczyk answer is the right one.