Clojure

eval reader fail to recognize function object

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.3
  • Fix Version/s: None
  • Component/s: None
  • Labels:

Description

(defmacro stubbing [stub-forms & body]
(let [stub-pairs (partition 2 stub-forms)
returns (map last stub-pairs)
stub-fns (map constantly returns) ;;(map #(list 'constantly %) returns)
real-fns (map first stub-pairs)]
`(binding [~@(interleave real-fns stub-fns)]
~@body)))

This macro is from Clojure In Action , whith the commented line rewrited (I know that original is better)
I assumed that this macro would work as supposed , if the stub forms use compile time literal, for e.g

(def ^:dynamic $ (fn [x] (* x 10))

(stubbing [$ 1]
($ 1 1))
;; =>
IllegalArgumentException No matching ctor found for class clojure.core$constantly$fn__3656
clojure.lang.Reflector.invokeConstructor (Reflector.java:166)
clojure.lang.LispReader$EvalReader.invoke (LispReader.java:1031)
clojure.lang.LispReader$DispatchReader.invoke (LispReader.java:618)

;;macro can expanded
(macroexpand-all '(stubbing [$ 1]
($ 1 1)))
;; =>
(let* [] (clojure.core/push-thread-bindings (clojure.core/hash-map (var $) #<core$constantly$fn_3656 clojure.core$constantly$fn_3656@161f888>)) (try ($ 1 1) (finally (clojure.core/pop-thread-bindings))))

I thought there is something wrong with eval reader, but i can't figure it out

btw the above code can run on clojure-clr

Activity

Hide
Naitong Xiao added a comment -

I use clojure 1.3

The example in Clojure In Action is Ok on clojure 1.3
I just found this peculiar thing when trying to answer a question from another one in a mail list.

Show
Naitong Xiao added a comment - I use clojure 1.3 The example in Clojure In Action is Ok on clojure 1.3 I just found this peculiar thing when trying to answer a question from another one in a mail list.
Hide
Michael Klishin added a comment -

As far as I know, Clojure in Action examples are written for Clojure 1.2. What version are you using?

Show
Michael Klishin added a comment - As far as I know, Clojure in Action examples are written for Clojure 1.2. What version are you using?

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: