看起來像defmulti
正在緩存調度功能。這裏是你的代碼的修改版本,說明了這個問題:
;; simple fn to resolve defmethod to call, hardcoded to :do-it
(defn who-is-it [person] (:name person))
(spyx (who-is-it {:name :joe}))
(defmulti do-something who-is-it)
(defmethod do-something :homer [person] :doh)
(defmethod do-something :bill [person] :oh-no)
(defmethod do-something :ted [person] :excellent)
(spyx (do-something {:name :homer}))
(spyx (do-something {:name :bill}))
;; now change who-is-it
(defn who-is-it [arg] :ted)
(spyx (who-is-it :wilma)) ;; expected result = :excellent
(spyx (do-something {:name :betty}))
與結果:
:reloading (tst.clj.core)
(who-is-it {:name :joe}) => :joe
(do-something {:name :homer}) => :doh
(do-something {:name :bill}) => :oh-no
(who-is-it :wilma) => :ted
:error-while-loading tst.clj.core
Error refreshing environment: java.lang.IllegalArgumentException: No method in multimethod 'do-something' for dispatch value: :betty, compiling:(tst/clj/core.clj:22:27)
看起來你可能需要重新初始化REPL重新定義調度FN。與我們看到預期的行爲的新會話
(defmulti do-something who-is-it)
(defmethod do-something :homer [person] :doh)
(defmethod do-something :bill [person] :oh-no)
(defmethod do-something :ted [person] :excellent)
(spyx (do-something {:name :betty})) ;=> ***same error ***
Error refreshing environment: java.lang.IllegalArgumentException: No method in multimethod 'do-something' for dispatch value: :betty, compiling:(tst/clj/core.clj:30:1)
這裏:
;; simple fn to resolve defmethod to call, hardcoded to :do-it
(defn who-is-it [person] (:name person))
(spyx (who-is-it {:name :joe}))
;; now change who-is-it
(defn who-is-it [arg] :ted)
(spyx (who-is-it :wilma)) ;; expected result = :ted
; (spyx (do-something {:name :betty}))
(defmulti do-something who-is-it)
(defmethod do-something :homer [person] :doh)
(defmethod do-something :bill [person] :oh-no)
(defmethod do-something :ted [person] :excellent)
(dotest
(spyx (do-something {:name :betty})))
(do-something {:name :betty}) => :excellent ; *** as expected ***
更新
我試過ns-unmap
技術Rumid描述和它的作品甚至重複一切並沒有爲我改寫do-something
也。我注意到,你必須重新發出都的defmulti
和所有defmethod
聲明:
(ns-unmap *ns* 'do-something) ; be sure to remember the quote
(defmulti do-something who-is-it)
(defmethod do-something :homer [person] :doh)
(defmethod do-something :bill [person] :oh-no)
(defmethod do-something :ted [person] :excellent)
(dotest
(newline)
(spyx (do-something {:name :betty}))) ;=> :excellent
哪裏是spyx從? – Kris