(1) #( ... ) is tanslated to (fn* [ ARGS ] ( ... )) before any macros
are expanded because it is a *reader macro*.

(2) The contents of #() are taken to be a function application. That's
why you can write #(+ 1 %) and have + applied to 1 and %. By the same
token, you can't write #(true) and expect it to work because that
expands to applying the function true to no arguments. That can't work
because true is not a function.

Again:
#( + 1 %)
(fn* [x] (+ 1 x) )
         ^     ^

#(true)

(fn* [] (true) )
        ^    ^
       don't overlook these parens!

 // ben


2011/1/30 Phillip Calçado <pcalc...@gmail.com>:
> Hi all,
>
> I've stumbled upon some interesting behaviour regarding macros and
> anonymous functions. I couldn't find doco anywhere on this so if you
> have any pointers please let me know.
>
> Considering the macro:
>
> (defmacro splicer [a] `(list ~@ a))
>
> What's the expected result of:
>
> (macroexpand-1 '(splicer #(true)))
>
> ?
>
> What I am getting now is:
>
> behaviors.t-canary> (macroexpand-1 '(splicer #(true)))
> (clojure.core/list fn* [] (true))
> behaviors.t-canary> (macroexpand-1 '(splicer (list true)))
> (clojure.core/list list true)
>
> I've found this issue while using Midje to test my code. This simple test 
> case:
>
> (fact "splice for lambdas"
>      (let [a #(true)]
>        (a) => true))
>
> Expands to:
>
> behaviors.t-canary> (macroexpand-1 '(fact "splice is broken for lambdas"
>      (let [a #(true)]
>        (a) => true)))
> (midje.util.wrapping/midje-wrapped (clojure.core/every?
> clojure.core/true? (clojure.core/list "splice is broken for lambdas"
> (let [a (fn* [] (true))] (midje.util.wrapping/midje-wrapped
> (midje.semi-sweet/expect (a) midje.semi-sweet/=> true :file-position
> (midje.util.file-position/line-number-known 1)))))))
>
>
> And results in:
> ;.;. FAIL at (NO_SOURCE_FILE:1)
> ;.;.     Expected: true
> ;.;.       Actual: java.lang.ClassCastException: java.lang.Boolean
> cannot be cast to clojure.lang.IFn
> ;.;.               
> behaviors.t_canary$eval7514$a__7515.invoke(NO_SOURCE_FILE:1)
> ;.;.               
> behaviors.t_canary$eval7514$fn__7517.invoke(NO_SOURCE_FILE:1)
>
> Can anyone shed a light on this? Expected behaviour, bug or am I
> missing something?
>
> Cheers
> --
> Phil Calçado
> http://fragmental.tw
> http://www.fragmental.com.br
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to