How to unload a function from another namespace?
I am loading a function say-hi
from the learning.greeting namespace
(use 'learning.greeting)
When I try to override the say-hi function under the current (custom) namespace, I got the error:
CompilerException java.lang.IllegalStateException: say-hi already refers to: #'learning.greeting/say-hi in namespace: user, compiling:(NO_SOURCE_PATH:1:1)
So how do you unload a function from other namespaces?
source to share
If you want to get rid of direct mapping to Var from another namespace in the REPL, say
(ns-unmap 'current-namespace 'local-alias)
Example:
user=> (ns-unmap *ns* 'reduce) nil user=> (reduce + 0 [1 2 3]) CompilerException java.lang.RuntimeException: Unable to resolve symbol: reduce in this context, compiling:(NO_SOURCE_PATH:2:1)
The local alias will be different from the actual Var name if used :rename
:
(use '[clojure.walk
:only [keywordize-keys]
:rename {keywordize-keys keywordize}])
To remove all mappings pointing to Vars in clojure.walk
:
(doseq [[sym v] (ns-map *ns*)]
(if (and (var? v)
(= (.. v -ns -name) 'clojure.walk))
(ns-unmap *ns* sym)))
source to share
Are you sure you want to remove say-hi
from learning.greeting
? If not, it is best used require
in this situation. Instead, (use 'learning.greeting)
do:
(require `[learning.greeting :as lg])
Then you can reference the original definition as lg/say-hi
and you can define the new version in the current namespace eg. and
(def say-hi [x] (lg/say-hi (list x x))
(I don't know if it makes sense for a function say-hi
, but the general point doesn't change independently.)
source to share
both use
and require
have an: exclude parameter for this situation:
(use '[learning.greeting :exclude [say-hi]])
or more preferably:
(require '[learning.greeting :refer :all :exclude [say-hi]])
or when you are working in a regular namespace everything is preferable in the form ns
:
(ns my-namespace
(:require [learning.greeting :refer [ function1 function2] :as greeting]
source to share