Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a multimethod in separate files in different namespace

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?

like image 569
Abhiroop Sarkar Avatar asked Jul 12 '16 06:07

Abhiroop Sarkar


1 Answers

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")
like image 137
SerCe Avatar answered Oct 22 '22 14:10

SerCe