简体   繁体   English

如何在Clojure中取消定义函数?

[英]How can I undefine a function in Clojure?

Often I need to undefine a function in clojure. 通常我需要在clojure中取消定义函数。 If I define something with defn how can I undefine it? 如果我用defn定义一些东西我怎么能取消它呢?

There is no one-argument version, because the same Var can be mapped in more than one namespace. 没有单参数版本,因为可以在多个名称空间中映射相同的Var。 If you are working from the REPL, you often want to unbind from the user namespace, eg 如果您使用REPL,则通常需要取消绑定用户命名空间,例如

(ns-unmap 'user 'symbol-to-unbind)

The first argument to ns-unmap can be a symbol or a namespace, and the second argument should be a symbol. ns-unmap的第一个参数可以是符号或命名空间,第二个参数应该是符号。

I think, that you can use ns-unmap to do this. 我想,您可以使用ns-unmap来执行此操作。

PS Couldn't add this code into comment, so i put it here. PS无法将此代码添加到评论中,所以我把它放在这里。 To unmap function in current namespace, you need to use following code: 要在当前命名空间中取消映射函数,您需要使用以下代码:

(ns-unmap *ns* 'method) 

If you have: 如果你有:

(def x 42)

It might be useful to unbind the var: 取消绑定var可能很有用:

(.unbindRoot #'x)

Now, if you try this 现在,如果你试试这个

x

You get: 你得到:

#<Unbound Unbound: #'user/x>

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

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