我是Clojure的初学者。我正在执行两次一个操作,但更改了符号lang
反对language
一种情况运行良好,另一种情况是抛出错误: java.lang.IllegalArgumentException: No method in multimethod 'my-method' for dispatch value: null
我不确定这是由 Clojure 语法引起的,还是我的 Linux 配置有问题。我有 Debian Stretch 和 boot.clj。
错误发生在终端中。在这里,您既是代码的和平,也是错误的:
s@lokal:~$ boot repl
nREPL server started on port 36091 on host 127.0.0.1 - nrepl://127.0.0.1:36091
java.lang.Exception: No namespace: reply.eval-modes.nrepl found
REPL-y 0.4.1, nREPL 0.4.4
Clojure 1.8.0
OpenJDK 64-Bit Server VM 1.8.0_181-8u181-b13-2~deb9u1-b13
Exit: Control+D or (exit) or (quit)
Commands: (user/help)
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Find by Name: (find-name "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
(user/clojuredocs name-here)
(user/clojuredocs "ns-here" "name-here")
boot.user=> (do
#_=> (defmulti my-method (fn[x] (x "lang")))
#_=> (defmethod my-method "English" [params] "Hello!")
#_=> (def english-map {"id" "1", "lang" "English"})
#_=> (my-method english-map)
#_=> )
"Hello!"
boot.user=>
boot.user=> (do
#_=> (defmulti my-method (fn[x] (x "language")))
#_=> (defmethod my-method "English" [params] "Hello!")
#_=> (def english-map {"id" "1", "language" "English"})
#_=> (my-method english-map)
#_=> )
java.lang.IllegalArgumentException: No method in multimethod 'my-method' for dispatch value: null
boot.user=>
我必须补充一点,在它适用于language
但不适用于lang
之前.当我用 mymetho-d
或 greeting
更改my-method
符号名称时,它也转向工作或不工作。
>defmulti
定义了一个 var,随后对具有相同名称的 defmulti
的调用不执行任何操作,因此您的第二次defmulti
调用无效,并且保留原始调度函数。删除defmethod
定义有remove-method
和remove-all-methods
,但要删除defmulti
定义(无需重新启动 REPL),您可以使用alter-var-root
将 var 设置为 nil:
(defmulti my-method (fn [x] (x "lang")))
(defmethod my-method "English" [params] "Hello!")
(def english-map {"id" "1", "lang" "English"})
(my-method english-map)
=> "Hello!"
(alter-var-root #'my-method (constantly nil)) ;; set my-method var to nil
(def english-map {"id" "1", "language" "English"})
(defmulti my-method (fn [x] (x "language")))
(defmethod my-method "English" [params] "Hello!")
(my-method english-map)
=> "Hello!"
您可以使用ns-unmap
达到类似的效果:
(ns-unmap *ns* 'my-method)