by mcav · 13y ago · 0 comments
(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.