Block Comments in Clojure

ClojureComments

Clojure Problem Overview


How do I comment multiple lines in Clojure?

Clojure Solutions


Solution 1 - Clojure

Actually, there is a way!


(comment




(defn hey []
("Hey there!"))




Check me out!
)

Just wrap your comments in (comment ..) :)

Have fun!

Solution 2 - Clojure

Clojure supports a #_ reader macro which completely skips the next form. This is mentioned on the page about the Clojure Reader. There is also the comment macro which has a similar effect, but is implemented differently.

Both the above require that the thing that you're commenting out is otherwise a syntactically correct S-expression.

Some Lisp dialects have a multi-line comment that can contain arbitrary text, but I don't see one for Clojure.

Solution 3 - Clojure

Double quotes (string literal) allow adding arbitrary text (not only proper S-forms):

(comment "

public class HelloWorld {
    public static void main(String[] args) {
        System.out.print("Hello, World");
        System.out.println();
    }
}

")

Solution 4 - Clojure

Other examples are great, I'd just like to add one more trick:

Sometimes you want to comment out a few lines of code, but still have the compiler compile it and report any errors (e.g. a set of commands in a top-level namespace that you plan to execute later at the REPL).

In this case I like to wrap the code with (fn [] .....) which means that it still gets compiled, it just doesn't get called.

Solution 5 - Clojure

See this link: http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips

You can create multiline comments with the syntax

(comment .....
    ....)

Solution 6 - Clojure

When using emacs and cider, there's a command M-x comment-region which I use often.

Solution 7 - Clojure

There are multiple ways that I know:

First one is using the comment macro: it only does not evaluates all the code inside the comment body (but it still checks for balanced parenthesis/brackets). If you know your way with paredit, it won't take much time if you want to comment a few sexp calls.

(comment 
 (println 1))

However, it will still check for parenthesis match. So if you have unbalanced parenthesis, you code won't compile (yielding java.lang.RuntimeException: EOF while reading).

Another way is using #_ (aka the discard macro): it will discard the next sexp, which is the way I personally prefer (faster to type and normally I do that on sexps when I have to debug):

#_(println 1)

It also checks for unmatched delimiters: so if you have unbalanced parenthesis, it won't compile as well.

Lastly, there is the ; character which will comment the line (similar to the other languages commentary feature) and the compile will ignore it completely. If you want to comment multiple lines, you need to prepend all the lines with ; , which are normally a hassle, but usually text editors will do it for you with a command after selecting multiple lines.

;  (println 1)
;  (println 1 also won't break

Solution 8 - Clojure

In Emacs you can use the command M-; (the shortcut for M-x comment-dwim). It will comment or uncomment any marked region of text/code, so, as long as your entire function or set of functions is included in the region, you can comment or uncomment that region quite easily. The Emacs reference manual for the function M-; can be found here.

Solution 9 - Clojure

For a long comment block, Macros #_ or (comment ...) didn't work as proprietarily, then I did comment block with VSCODE(OS X).

  1. Highlight the comment block.
  2. press command+shift+p in vscode.
  3. find "Add Line Comment"

Solution 10 - Clojure

Clojure also supports a double #_ #_ reader macro which completely skips the next two forms. Very useful for commenting out a key and multi-line value in a map.

Example

{
:db-connector "jdbc"
#_ #_ :auto-offset-reset    {:dev     "earliest"
                             :default "latest"}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionunj2View Question on Stackoverflow
Solution 1 - ClojureRayneView Answer on Stackoverflow
Solution 2 - ClojureGreg HewgillView Answer on Stackoverflow
Solution 3 - ClojureIvan SviatenkoView Answer on Stackoverflow
Solution 4 - ClojuremikeraView Answer on Stackoverflow
Solution 5 - ClojurechollidaView Answer on Stackoverflow
Solution 6 - ClojureclajView Answer on Stackoverflow
Solution 7 - ClojureRodrigo FloresView Answer on Stackoverflow
Solution 8 - ClojureMLevView Answer on Stackoverflow
Solution 9 - ClojuremadeinQuantView Answer on Stackoverflow
Solution 10 - ClojureCambodianCoderView Answer on Stackoverflow