by kleiba · 13y ago · 0 comments
(defn foo
([x] "bar")
([x y] "baz"))
? (defn foo
([x] *docstring* "bar")
([x y] *docstring* "baz"))
because you want to be able to document the different implementations differently. The semantics would be: If the function body starts with a string literal, the latter serves as the documentation. If the body consists of any other code following the string literal, it will be ignored for run-time processing; if it's the only thing in the body, it also serves as the return value.I disagree with this. Since the normal functions cannot dispatch on type⁺, only arity, the different implementations are typically very related and should share same documentation. Your proposal would lead to massive duplication, and reduce the likelihood of people actually documenting the functions.
⁺ There are two separate mechanisms for providing richer function dispatch, both allow more granular documentation.
However, if this only applies to different arities, please also compare to the following style:
(defun function (a b &optional c d e)
"Documentation string"
"return value"))
I still think that the function signature is more readable when the doc string comes after the parameters, but perhaps it's just a matter of taste.The above is obviously not Clojure. But as I commented originally, I just wasn't aware that this is the way Clojure does it.
(defn some-string []
"is this a docstring or a return value?")Clojure docstrings need to precede the argument list. e.g. this is correct:
(defn foo
"Some docstring"
[]
"Some return value"))
But this is not: (defn foo
[]
"Some docstring"
"Some return value"))
It will still compile, but the first string will be treated as an expression, and therefore ignored as it has no side effects.The reason docstrings in Clojure come before the arguments, is that because if they came after, their effect would be ambiguous, regardless of arity. e.g.
(defn foo [] "foo")
Is the string supposed to be the return value of the function, or its docstring?- If the function body starts with a string literal, it is always the doc-string
- If that string is the only thing in the function body, it is also the return value.