5

マルチメソッドとその実装を別のファイルで定義しようとしています。次のようになります: ファイル 1 内

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

ファイル 2 で

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

そして、メソッドを呼び出すときにメインファイルで次のように定義します。

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

これを行うと、例外が発生します"No method in multimethod 'foo'for dispatch value: hello

私は何を間違っていますか?それとも、別のファイルでマルチメソッドの実装を定義することはできませんか?

4

1 に答える 1

6

可能です。問題は、thing.a.b.c名前空間が読み込まれていないためです。使用する前にロードする必要があります。

これは正しい例です:

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

(test/foo "hello")
于 2016-07-12T08:44:00.813 に答える