Question

In Clojure, does the arity of a call to a *multimethod* need to match...

- the arity of its *dispatching function*?
- the arity of the method that will be called based on the *dispatching value*?

#clojure

Answer

(UPDATE: What follows only works in ClojureScript, not Clojure.)

No, the arity of a call to a multimethod...

- doesn't need to match the arity of the multimethod's dispatching function
- doesn't need to match the arity of the method that will be called based on the *dispatching value*

Ex.

(defmulti mm (fn [a b] a))
(defmethod mm :m1 [a] a)
(defmethod mm :m3 [a b c] [a b c])

(mm :m1)
; => :m1
(mm :m1 2)
; => :m1
(mm :m3)
; => [:m3 nil nil]
(mm :m3 1 2 3)
; => [:m3 1 2]
(mm :m3 1 2)
; => [:m3 1 2] 

[^1]: https://clojure.org/reference/multimethods
[^2]: http://stackoverflow.com/q/10313657

Learn

Author

xavi

Reviews
Last
next

Students 1