我是Clojure的初学者。我执行了两次操作,但是对ID VAL LENGTH SUM
1 1 1 1
1 1 1 1
1 1 2 2
1 1 2 2
2 0 1 0
2 3 1 0
2 4 2 3
更改了符号lang
一种情况下运行良好,另一种情况下则抛出错误:
language
我不确定这是由Clojure语法引起的,还是在我的Linux配置中有问题。我有Debian Stretch和boot.clj。
该错误发生在终端中。这是代码的两全其美和一个错误:
java.lang.IllegalArgumentException: No method in multimethod 'my-method' for dispatch value: null
我必须添加它,然后才能与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
或my-method
更改mymetho-d
符号名称时,它还是可以正常工作。
答案 0 :(得分:3)
defmulti
定义了一个变量,随后对具有相同名称的defmulti
的调用没有任何作用,因此您的第二个defmulti
调用无效,并且保留了原来的调度功能。有remove-method
和remove-all-methods
可以删除defmethod
的定义,但是要删除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)