defmulti does not locate defmethod in another namespace

Asked

Viewed 22 times

0

I created a clojure project to validate defmulti Feature with defmethod implementations in other namespace. However, when running the defmulti Dispatcher project it does not locate the correct implementation.

url of the project in git

Implementation

(ns multi-metodos.executar)

(defmulti run :tipo)
(ns multi-metodos.sms
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :default [_]
  (println "default"))
(ns multi-metodos.notificacao
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :notificacao [_]
  (println "Enviando notificacao"))
(ns multi-metodos.email
  (:require [multi-metodos.executar :as exe]))

(defmethod exe/run :email [_]
  (println "Enviando email"))
(ns multi-metodos.core
  (:require [multi-metodos.executar :as exe]))

(defn -main []
  (println "Ola")
  (exe/run {:tipo :email}))

error generated:

Exception in thread "main" Syntax error compiling at (C:\Users\guilherme.angelico\AppData\Local\Temp\form-init7125751530945241762.clj:1:121).
    at clojure.lang.Compiler.load(Compiler.java:7647)
    at clojure.lang.Compiler.loadFile(Compiler.java:7573)
    at clojure.main$load_script.invokeStatic(main.clj:452)
    at clojure.main$init_opt.invokeStatic(main.clj:454)
    at clojure.main$init_opt.invoke(main.clj:454)
    at clojure.main$initialize.invokeStatic(main.clj:485)
    at clojure.main$null_opt.invokeStatic(main.clj:519)
    at clojure.main$null_opt.invoke(main.clj:516)
    at clojure.main$main.invokeStatic(main.clj:598)
    at clojure.main$main.doInvoke(main.clj:561)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.lang.Var.applyTo(Var.java:705)
    at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: No method in multimethod 'run' for dispatch value: :email
    at clojure.lang.MultiFn.getFn(MultiFn.java:156)
    at clojure.lang.MultiFn.invoke(MultiFn.java:229)
    at multi_metodos.core$_main.invokeStatic(core.clj:6)
    at multi_metodos.core$_main.invoke(core.clj:4)
    at clojure.lang.Var.invoke(Var.java:380)
    at user$eval140.invokeStatic(form-init7125751530945241762.clj:1)
    at user$eval140.invoke(form-init7125751530945241762.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:7176)
    at clojure.lang.Compiler.eval(Compiler.java:7166)
    at clojure.lang.Compiler.load(Compiler.java:7635)
    ... 12 more

1 answer

1

Iisso has been translated using Google Translate. I apologize if it is broken English

You need to ensure that the multi-metodos.email is loaded at some point. The email file is never loaded, so (defmethod exe/run :email [_] . . .) never runs.

You will need to import the email file to make it work. I admit I don’t use multi-methods very often, so I don’t know the best way to set this up. However, it can only be remedied by importing the email file:

(ns multi-metodos.core
  (:require [multi-metodos.executar :as exe]
            [multi-metodos.email]))  ; Verifique se está carregado

(defn -main []
  (println "Ola")
  (exe/run {:tipo :email}))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.