0

我是 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。当我用or更改my-method符号名称时,它也可以工作或不工作。mymetho-dgreeting

4

1 回答 1

3

defmulti定义了一个 var,随后defmulti对同名的调用什么也不做,所以你的第二次defmulti调用无效,原来的调度函数仍然存在。有remove-methodremove-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)
于 2019-01-27T16:22:14.190 回答