Abh*_*kar 3 polymorphism clojure multimethod
我试图在一个单独的文件中定义一个多重方法及其实现。内容如下:在文件1中
(ns thing.a.b)
(defn dispatch-fn [x] x)
(defmulti foo dispatch-fn)
Run Code Online (Sandbox Code Playgroud)
在文件2中
(ns thing.a.b.c
(:require [thing.a.b :refer [foo]])
(defmethod foo "hello" [s] s)
(defmethod foo "goodbye" [s] "TATA")
Run Code Online (Sandbox Code Playgroud)
在主文件中,当我调用该方法时,我会定义如下内容:
(ns thing.a.e
(:require thing.a.b :as test))
.
.
.
(test/foo "hello")
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我会说一个例外 "No method in multimethod 'foo'for dispatch value: hello
我究竟做错了什么?还是无法在单独的文件中定义多方法的实现?
有可能的。问题是因为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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
656 次 |
| 最近记录: |