Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么我们可以写
(defn factory-foo [] (fn [] (println "foo"))) (apply (factory-foo) [])
但不是:
(defn factory-bar [] #((println "bar"))) (apply (factory-bar ) []) ;throws NPE
这是一个错误吗?
#((println "bar))由读者翻译为(fn [] ((println "bar")))- 注意额外的括号。(println "bar")这里 printsbar并返回nil,然后nil由于外括号而将其本身作为函数调用。nil实际上null是,并且试图取消对 NPE 的引用。
#((println "bar))
(fn [] ((println "bar")))
(println "bar")
bar
nil
null
#(..)为了避免这种情况,只需在:中放置额外的括号#(println "bar")。
#(..)
#(println "bar")