Block Comments in Clojure

How do I comment multiple lines in Clojure?


Actually, there is a way!


(comment

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

Check me out! )

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

Have fun!


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.


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();
    }
}

")

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.