Block Comments in Clojure

元气小坏坏 提交于 2019-12-03 02:53:42

问题


How do I comment multiple lines in Clojure?


回答1:


Actually, there is a way!


(comment

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

Check me out! )

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

Have fun!




回答2:


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.




回答3:


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

")



回答4:


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.




回答5:


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

You can create multiline comments with the syntax

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



回答6:


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




回答7:


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.




回答8:


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


来源:https://stackoverflow.com/questions/1191628/block-comments-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!