在玩 Clojure 时,我注意到 ('+ 2 2) 并没有像我预期的那样抛出错误——它返回 2。我花了几分钟玩弄:
(def f (cast clojure.lang.IFn 'a-symbol))
(f 5) ;; => nil
(f 5 5) ;; => 5
(f 5 5 5) ;; ArityException Wrong number of args (3) passed to: Symbol
(f "hey") ;; => nil
(f "foo" "bar") ;; => "bar"
(f "foo" "bar" "baz") ;; => ArityException Wrong number of args (3) passed to: Symbol
据我所知,符号被强制转换为名为 Symbol 的函数,该函数接受两个参数并返回第二个参数。我猜它与符号类的实现有关?