[英]multi-arity defn in Clojure — first match first serve?
To be concrete, what is supposed to happen in the following situation: 具体来说,在以下情况下应该发生什么:
(defn avg
([] 0)
([& args] (/ (reduce + args) (count args))))
(avg)
ie, can I rely on clojure to always return 0 rather than divide-by-zero? 即,我可以依靠clojure总是返回0而不是被零除吗?
You can rely on Clojure to return 0 rather than divide-by-zero. 您可以依靠Clojure返回0,而不是除零。 But it isn't first match, first served:
但这不是第一次比赛,先到先得:
(defn avg
([& args] (/ (reduce + args) (count args)))
([] 0))
(avg)
; 0
The specific arities take precedence over the rest argument, as described here . 具体arities优先于其他参数,如所描述这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.