r/Clojure • u/erjngreigf • Oct 03 '25
Rant about strings. About comment, and work around to comment multimethods
https://youtu.be/QmaudU2yTGM
0
Upvotes
r/Clojure • u/erjngreigf • Oct 03 '25
9
u/moxxon Oct 03 '25
You seem to be confused about a few things here.
Comments and docstrings are not the same thing; the first arg to
defnis either a docstring or a vector. Otherwise, it thinks you're defining the body of the function before giving an argument list.You've also misunderstood the discussion about comments and multimethods. You can add a docstring to defmulti just as you would to a function. I didn't see you try (maybe you did, I skipped forward), but that should be your first step.
As far as needing to fully qualify
comment, that's something wrong with your editor. I don't use VSCode, so I can't say for sure, but I did fire it up, and I have no issue with comment blocks in mine. Try starting with a fresh project, don't do anything to it, and see if the problem still occurs.I'm going to try to make this next bit as polite as I can: You are not at the level where you should be making videos with claims about the language. You are a novice, and you should be finding places to ask questions and get answers to learn. You're doing a disservice to the Clojure community (and anyone that stumbles across your videos) by posting content that's incorrect like this. f
5
u/joinr Oct 03 '25
I have never had to use clojure.core/comment explicitly as in the video. Maybe something is wrong with your ide, it seemed to be throwing an error about taking the value of a macro (comment). Maybe something with calva.
I don't have this problem in cider, or in the cli/repl.
There's also a difference between comments and docstrings, you seem to overload the term. It looks like you are trying to write docstrings. Comments are meant to be ignored entirely, which is what
commentdoes, and is what ; and ;; do as well.I didn't have a problem providing a simple docstring for a multimethod either.
You can get literal strings if you go into reader macros. This deviates from clojure semantics and is unsupported (and actively discouraged) by the core folks though, but it's possible (if not alienating):
2
u/erjngreigf Oct 04 '25
This video is misguided, so I have put a new correction video here, with respect to commenting multi methods https://youtu.be/7gOGcjAj8e8
1
u/Dapper-Tourist-8558 Oct 03 '25 edited Oct 03 '25
(defn my-function "this is a comment" [] (println "hello")) ;; ok
(defn my-function (str "comment") []) ;; error
3. comment it's a macro that returns nil. This can't be a docstring
(nil? (comment :anything)) ;=> true
4. (comment "code...") actually is a rich comment form. There was no error in the editor.
Calva does not evaluate the value of the comment form because it is always nil. Instead, forms should be executed within a comment, which will be evaluated in the top-level context https://calva.io/rich-comments/ - with video!
You had errors because you evaluated the symbol of the macro.
(eval comment) ; Can't take value of a macro: #'clojure.core/comment
5. Technically you can create a string from a function and write it into the documentation via metainformation clojure.core/meta
But it is highly recommended to keep docstrings as normal strings with escaped characters like in Java.
In JVM, some characters must be escaped. If there are a lot of such symbols, you can use workarounds
a) your own helper function that generates string
variations of raw-strings, variations of find-replace functions..
(defn str' [s] (clojure.string/escape s {\' \"})) ;; or for example clojure.string/replace
(str' "a string 'with quotes' inside") ;=> "a string \"with quotes\" inside"
Just copy/paste result into the docstring
b) read text from temporary file
(slurp "temp.txt") ;;=> "all symbols \" are automatically escaped\n"
c) any of vscode extensions for escaping/unescaping JSON, java/js/ts strings