Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block Comments in Clojure

People also ask

How to comment out in Clojure?

; (semicolon) reader macro This is used to comment out a single line. Clojure ignores everything from the ; (semicolon) to the end of the line.

How do you comment multiple lines in Clojure?

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.


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.