【问题标题】:General syntax of multimethods多方法的一般语法
【发布时间】:2012-03-31 22:22:07
【问题描述】:
如果问题是微不足道的,我深表歉意,但一些谷歌搜索并没有引导我到任何地方。 defmulti 和 defmethod 的一般语法是什么?我可以编写简单的多方法,但我不确定在哪里可以放置文档字符串、前置条件和后置条件、元数据等。
实际上我对 ClojureScript 的兴趣比对 Clojure 的多,所以如果两者之间存在差异,请告诉我。
【问题讨论】:
标签:
clojure
clojurescript
multimethod
【解决方案1】:
在 REPL 中,您可以使用 doc 函数来获取函数参数和(大多数情况下)选项的解释。对于 ClojureScript,这两个函数是宏,这意味着它们会在编译时扩展,并且应该与常规 Clojure 中的行为完全相同。也就是说,只要 ClojureScript 可以处理宏生成的代码。
user=> (doc defmulti)
-------------------------
clojure.core/defmulti
([name docstring? attr-map? dispatch-fn & options])
Macro
Creates a new multimethod with the associated dispatch function.
The docstring and attribute-map are optional.
Options are key-value pairs and may be one of:
:default the default dispatch value, defaults to :default
:hierarchy the isa? hierarchy to use for dispatching
defaults to the global hierarchy
nil
user=> (doc defmethod)
-------------------------
clojure.core/defmethod
([multifn dispatch-val & fn-tail])
Macro
Creates and installs a new method of multimethod associated with dispatch-value.
nil