Two ways of commenting out in Hy

Sometimes it’s useful to be able to comment some code. It might be that you want to leave a mental note behind or want to quickly test something. Hy currently supports two (that I know of) ways of doing this.

First way is using ; character. It causes Hy to ignore rest of the line. I prefer using double (ie. ;;) when commenting out a whole line. It doesn’t matter to hy, but it’s just a habit I picked up at the beginning. Comments can appear on their own line or at the end of a line like shown in the example:

;; this is an example
(defn complicated-function [a b c]
  ;; comments inside of function can be sometimes useful
  ;; but I try to avoid having too long ones
  (-> (decouple-capacitors [a b]) ; important!
      (load-flux-scrubber [b c])
      (reverse-polarity (jeffries-coeff a b) c)))

And of course, any code that is in comments, isn’t parsed by Hy:

;; (defn example-fn [a b]
;;   (-> (transmogrify a b)
;;       (upgrade-result b)))

While this works well with small batches of code, it soon gets really tedious to comment out large functions. Especially if you’re using an editor that likes to balance parentheses. For such cases #_ macro is a better option.

#_ macro is a special one that completely discards its input. Not even None is emitted. The end result is nothing at all. The previous example, where example-fn was commented out completely, can be achieved in the following way:

#_(defn example-fn [a b]
    (-> (transmogrify a b)
        (upgrade-result b)))

There’s one big difference. ; instructs hy to ignore rest of the line, while #_ is a macro that doesn’t output anything. This means that ; can be used to comment out anything, but #_ must be followed by a syntactically legal expression:

;; this is possible
; (defn foo .. I forgot that to write here

;; this is not possible
#_(defn foo .. I forgot that to write here
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s