2016-07-12 36 views
2

我想在一个单独的文件中定义multimethod及其实现。它是这样的: 在文件1在不同命名空间的单独文件中实现multimethod

(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

我在做什么错了?或者是不可能在单独的文件中定义multimethods的实现?

回答

4

这是可能的。问题是因为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") 
相关问题