In the documentation for Clojure special forms (http://clojure.org/special_forms) the example for :pre and :post looks like this:
(defn constrained-sqr [x]
{:pre [(pos? x)]
:post [(> % 16), (< % 225)]}
(* x x))
How can Clojure tell if the map containing the meta-data is not the definition of the function? Shouldn't the meta-data precede the params vector? According to the documentation for defn (http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/defn), the syntax is
(defn name doc-string? attr-map? [params*] body)
with the attr-map? before the params vector. Isn't this more correct:
(defn constrained-sqr
{:pre [(pos? x)]
:post [(> % 16), (< % 225)]}
[x]
(* x x))
Should I file a bug report or am I misreading this?
Sorry to pick nits.
The map has to follow the argument vector because it depends on the arguments. And you can have multiple function bodies with different arities. The disambiguation is pretty simple: is the map the only thing in the body, it's the return value. If there was something else following it, the map would be thrown away. Then add it to the metadata. This is a very straight-forward and pretty simple heuristic which shouldn't clash with any production code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With