6

I am trying to define a multimethod and its implementation in a separate file. It goes something like this: In file 1

(ns thing.a.b)
(defn dispatch-fn [x] x)
(defmulti foo dispatch-fn)

In file 2

(ns thing.a.b.c
  (:require [thing.a.b :refer [foo]])
(defmethod foo "hello" [s] s)
(defmethod foo "goodbye" [s] "TATA")

And in the main file when I am calling the method I define something like this:

(ns thing.a.e
  (:require thing.a.b :as test))
.
.
.
(test/foo "hello")

When I do this I get an exception saying "No method in multimethod 'foo'for dispatch value: hello

What am I doing wrong? Or is it not possible to define implementations of multimethods in separate files?

1 Answer 1

7

It is possible. The problem is because thing.a.b.c namespace isn't loaded. You have to load it before using.

This is a correct example:

(ns thing.a.e
  (:require
    [thing.a.b.c] ; Here all your defmethods loaded
    [thing.a.b :as test]))

(test/foo "hello")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.