In clojure, how do I need a multimethod?

I know I can do (:use function) , but how to do it for a multimethod?

+4
source share
1 answer

Multimethods are used from other namespaces in the same way as functions.

If you have the following in com / example / foo.clj

 (ns com.example.foo) (defn f [x] (* xx)) (defmulti m first) (defmethod m :a [coll] (map inc (rest coll))) 

In the file com / example / bar.clj you can use both f and m as follows:

 (ns com.example.bar (:use [com.example.foo :only [fm]])) (defn g [] (println (f 5)) ; Call the function (println (m [:a 1 2 3]))) ; Call the multimethod ;; You can also define new cases for the multimethod defined in foo ;; which are then available everywhere m is (defmethod m :b [coll] (map dec (rest coll))) 

Hope this answers your question!

+7
source

All Articles