开始编辑:
认罪!我道歉。
我在 Clojure 1.2.1 的 cake repl 中运行它,老实说它不起作用。现在它在退出 cake repl 和 cake compile 后就可以了,它也适用于 1.3.0。
结束编辑:
在下面:我的调度函数被传递了零参数,但我不知道为什么。我已经测试了 dispatch 函数,它做了它应该做的。如果有任何建议,我将不胜感激。
(defrecord AcctInfo [acct-type int-val cur-bal])
(def acct-info (AcctInfo. \C 0.02 100.00))
ba1-app=> acct-info
ba1_app.AcctInfo{:acct-type \C, :int-val 0.02, :cur-bal 100.0}
(defn get-int-calc-tag [acct-type]
(cond (= acct-type \C) :checking
(= acct-type \S) :savings
(= acct-type \M) :moneym
:else :unknown))
(defmulti calc-int (fn [acct-info] (get-int-calc-tag (:acct-type acct-info))))
(defmethod calc-int :checking [acct-info] (* (:cur-bal acct-info) (:int-val acct-info)))
ba1-app=> (get-int-calc-tag (:acct-type acct-info))
:checking
ba1-app=> (calc-int acct-info)
java.lang.IllegalArgumentException: Wrong number of args (0) passed to: ba1-app$get-int-calc-tag
最佳答案
这个问题可能与 defmulti
的未记录的 defonce
行为有关。
如果您重新加载包含 (defmulti foo ...)
形式的命名空间,则该 defmulti
将不会更新。这通常意味着分派(dispatch)函数不会更新,但所有方法实现(在同一命名空间中)都会更新。 (defmulti foo ...)
如果 foo
var 已经绑定(bind)到一个值,则不执行任何操作。
要在 REPL 中修复此问题,请删除 multimethod var (ns-unmap 'the.ns 'the-multimethod)
然后重新加载命名空间 (require 'the.ns :reload )
。
为了防止这个问题,你可以单独定义调度函数并将其 var 传递给 defmulti
,如下所示:
(defn foo-dispatch [...]
...)
(defmulti foo #'foo-dispatch)
当代码看起来像这样时,如果您对 foo-dispatch
进行更改,则足以重新加载命名空间。
关于clojure - 为什么 Clojure defmulti 调度函数接收 0 个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8565044/