这是一个关于 Clojure 与 Haskell 等其他语言的表达能力的问题。更广泛的问题是解决表达问题 http://en.wikipedia.org/wiki/Expression_problem
这个问题 https://stackoverflow.com/questions/22040115/what-are-the-reasons-that-protocols-and-multimethods-in-clojure-are-less-powerfu得出的结论是,一般来说,Clojure 协议(和多方法)的表达能力不如 Haskell 类型类,因为协议在第一个参数上分派,而 Haskell 类型类可以在返回类型上分派。 (现在我觉得这个推理真的很有趣,并没有兴趣发起一场语言战争。我只是对思路清晰感兴趣)。
作为打破这个推理的一部分 - 我的问题是 -我们不能制作一个按返回类型分派的 Clojure 多方法吗(或键入提示)。我认为我们可以将以下表达式放入 Clojure 多方法中:
(= java.lang.String (:tag (meta #'my-string)))
其中函数是:
(defn ^String my-string []
"hello world")
编辑:
重点是我可以运行:
(meta #'my-string)
不进行函数求值,得到如下结果:
{:arglists ([]), :ns #<Namespace push-price.core>, :name my-string, :column 1,
:line 1, :file "/private/var/folders/0l/x6hr0t1j2hvcmm_sqq04vdym0000gn/T/form-
init7576840885484540032.clj", :tag java.lang.String}
即我有一些关于我的函数的预期类型的信息,但没有对其进行评估。
编辑 3(2014 年 4 月 24 日):
假设我有以下类型:
(deftype 字符串类型 [])
(deftype int-type [])
然后我根据这些类型定义了以下函数:
(defn #^{:return-type string-type} return-string []
"I am returning a string")
(defn #^{:return-type int-type} return-int []
42)
现在我编写一个函数来调度它们的返回类型,如下所示:
(defn return-type-dispatch [fn-arg]
(let [return-type (:return-type (meta fn-arg))]
(cond
(= return-type string-type)
"This function has a return type of the string type"
(= return-type int-type)
"This function has a return type of the integer type"
:else (str "This function has a return type of:" return-type))))
然后我编写一个宏在编译时运行它
(defmacro compile-time-type-check-string []
(println (return-type-dispatch #'return-string)))
(compile-time-type-check-string)
然后我像这样测试它:
lein uberjar
这给出了以下结果:
$ lein uberjar
Compiling dispatch-type-compile-time.core
This function has a return type of:class dispatch_type_compile_time.core.string-type
...
所以我似乎正在按返回类型进行调度。