yazz.com
yazz.com

Reputation: 58786

In clojure how do I require a multimethod?

I know I can do (:use function) but how do I do this for a multimethod?

Upvotes: 5

Views: 342

Answers (1)

raek
raek

Reputation: 2214

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]
  (* x x))

(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 in the same manner:

(ns com.example.bar
  (:use [com.example.foo :only [f m]]))

(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)))

I hope this answers your question!

Upvotes: 8

Related Questions