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")here は出力barして を返し、外側の括弧のためにnilそれ自体が関数として呼び出されます。実際、それを逆参照しようとすると、NPE が発生します。nilnilnull
#((println "bar))
(fn [] ((println "bar")))
(println "bar")
bar
nil
null
これを避けるには、 : 内に余分な括弧のペアをドロップして#(..)ください#(println "bar")。
#(..)
#(println "bar")