What is #_

625 views
Skip to first unread message

Yang Dong

unread,
Aug 3, 2010, 10:45:00 AM8/3/10
to Clojure
Hi,

I've read the src of core.clj of Clojure 1.1.0. Originally I thought
the meaning of #_ is to comment the thing after it, sort of like `;'.
But the in the src of core.clj in 1.2.0-RC1. The definition of reduce
is overrided to use the internal-reduce function. The defn line, is
preceded by `#_'. But in 1.1.0, it's not preceded by this reader
macro. So, I'm confused...

Meikel Brandmeyer

unread,
Aug 3, 2010, 3:35:59 PM8/3/10
to clo...@googlegroups.com
Hi,

You are absolutely right. The #_ is kind of comment. And in fact the override with the internal reduce function is commented out (ie. it’s not active) in 1.2.

Sincerely
Meikel

Nikita Beloglazov

unread,
Aug 3, 2010, 3:54:48 PM8/3/10
to clo...@googlegroups.com
#_ comments the whole form, starting with parent just after #_ : #_(bla-bla (bla-bla2 ... ) )
for example
(+  #_ (+
            2 3 4 (* 1 2) )
      1 2)
Will return 3, because form with (+ 2 3 ... )  will be ignored by reader
So it's restricted by 1 line like ; 

Regards
Nikita Beloglazov


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Nikita Beloglazov

unread,
Aug 3, 2010, 3:59:32 PM8/3/10
to clo...@googlegroups.com
Sorry, it's NOT restricted by 1 line, like ;
:)
Regards
Nikita Beloglazov

Yang Dong

unread,
Aug 3, 2010, 8:32:24 PM8/3/10
to Clojure
Thank you! I have just another question not related about this topic:

;during bootstrap we don't have destructuring let, loop or fn, will
redefine later
(def
^{:macro true
:added "1.0"}
let (fn* let [&form &env & decl] (cons 'let* decl)))

In Clojure 1.2.0, the preceding definition of let taks the strange
argument of `&form' and `&env'. What are they?

Meikel Brandmeyer

unread,
Aug 4, 2010, 1:38:21 AM8/4/10
to Clojure
Hi,

On Aug 4, 2:32 am, Yang Dong <ydong.pub...@gmail.com> wrote:

> Thank you! I have just another question not related about this topic:
>
> ;during bootstrap we don't have destructuring let, loop or fn, will
> redefine later
> (def
>   ^{:macro true
>     :added "1.0"}
>   let (fn* let [&form &env & decl] (cons 'let* decl)))
>
> In Clojure 1.2.0, the preceding definition of let taks the strange
> argument of `&form' and `&env'. What are they?

user=> (defmacro foo [x y] (prn &form) (prn &env) `(+ ~x ~y))
#'user/foo
user=> (let [x 5] (foo x 6))
(foo x 6)
{x #<LocalBinding clojure.lang.Compiler$LocalBinding@13f991>}
11

&form contains the form how the macro was called. &env contains local
bindings in the macro environment. (At least this is what my little
test shows...) These magic parameters are given to all parameters, but
usually hidden. It's probably safe to ignore them if you don't know
what they really do.

Sincerely
Meikel

Ted Naleid

unread,
Aug 4, 2010, 1:43:27 AM8/4/10
to clo...@googlegroups.com
I've seen situations where #_ still affects the code (or at least allows it to compile).

reduce expects a function with 2 parameters, one for

If you try to use the #() macro, but only use the first implicit parameter (%1), it'll blow up with an IllegalArgumentException:

user=> (reduce #(+ %1 1) 0 [1 2 3 5 8])
java.lang.IllegalArgumentException: Wrong number of args passed to: user$eval--87$fn (NO_SOURCE_FILE:0)

If you add in #_ %2, and it will succeed as the compiler then knows that it's a 2 argument function:

user=> (reduce #(+ 1 %1 #_ %2) 0 [1 2 6 4 4])
5

(though, really, the intent is clearer if you just define the function with explicit args)

user=> (reduce (fn [i n] (+ i 1)) 0 [1 2 3 5 8])
5

There might be other situations though where #_ and the thing it "comments" can still have an impact on how the code executes, just something I ran across recently that slightly adjusted my thinking on #_.

-Ted

Yang Dong

unread,
Aug 4, 2010, 6:10:36 AM8/4/10
to Clojure
Thank you, that really helps!

Meikel Brandmeyer

unread,
Aug 4, 2010, 10:12:08 AM8/4/10
to Clojure
Hi,

On Aug 4, 7:43 am, Ted Naleid <cont...@naleid.com> wrote:

> There might be other situations though where #_ and the thing it "comments"
> can still have an impact on how the code executes, just something I ran
> across recently that slightly adjusted my thinking on #_.

Well... You always have to be careful.

(cond
(pred1? x) (frob z)
#_ (pred2? y) (yoyo dyne)
:else (ac me))

Easy to make a mistake.

Sincerely
Meikel
Reply all
Reply to author
Forward
0 new messages