我能理解这是如何运作的:
(defmulti area :Shape)
(defmethod area :B [x] (println "Rect"))
(defmethod area :C [x] (println "Circle"))
(defmethod area :default [x] (do (println "Unknown") (:Shape x)))
(area {:Shape :B})
=> Rect
nil
(area {:Shape :Bb})
=> Unknown
:Bb
但是,只需将:Shape
更改为:Shap
,我就会停止了解这是如何发送的:
(defmulti area :Shap)
(defmethod area :B [x] (println "Rect"))
(defmethod area :C [x] (println "Circle"))
(defmethod area :default [x] (do (println "Unknown") (:Shap x)))
(area {:Shap :B})
=> Unknown
:B
(area {:Shap :C})
=> Unknown
:C
带有逆时针插件的Eclipse上的Clojure 1.5.1
答案 0 :(得分:7)
Multimethods不喜欢使用新的调度功能重新定义。这是一个颇具争议的特征,但事情就是如此。为了改变多方法的调度功能,你必须首先将它定义为不是多方法的东西(例如,为零)。
(defmulti area :Shape)
(def area nil)
(defmulti area :Shap)