以下代码按预期方式执行,但在最后给出NullPointerException
。我在这里做错了什么?为什么我会在下面的代码中获得NPE?
(ns my-first-macro)
(defmacro exec-all [& commands]
(map (fn [c] `(println "Code: " '~c "\t=>\tResult: " ~c)) commands))
(exec-all
(cons 2 [4 5 6])
({:k 3 :m 8} :k)
(conj [4 5 \d] \e \f))
; Output:
; Clojure 1.2.0-master-SNAPSHOT
; Code: (cons 2 [4 5 6]) => Result: (2 4 5 6)
; Code: ({:k 3, :m 8} :k) => Result: 3
; Code: (conj [4 5 d] e f) => Result: [4 5 d e f]
; java.lang.NullPointerException (MyFirstMacro.clj:0)
; 1:1 user=> #<Namespace my-first-macro>
; 1:2 my-first-macro=>
(对于正确语法高亮代码,请here)
+1 jejej,获得使用这些括号:) – OscarRyz 2010-07-14 15:12:02
+1,任何其他方式来解决? – missingfaktor 2010-07-14 15:15:11
当然,你可以重写它以扩大到'doseq'等,但为什么?这是一个完全合理的解决方案,对现有代码的更改很少;我会说坚持下去。 – 2010-07-14 16:19:20