Implementing a multimethod in separate files in different namespace
Asked Answered
W

1

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?

Waterrepellent answered 12/7, 2016 at 6:13 Comment(0)
T
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")
Thielen answered 12/7, 2016 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.