原始函数仅在使用 defn
定义时工作(例如,返回原始类型)。使用 fn
创建的等效函数的行为与使用 defn
创建的函数不同。例如:
(definterface IPrimitiveTester
(getType (link: ^int x))
(getType (link: ^long x))
(getType (link: ^float x))
(getType (link: ^double x))
(getType (link: ^Object x)))
(deftype PrimitiveTester (link: )
IPrimitiveTester
(getType (link: this ^int x) :int)
(getType (link: this ^long x) :long)
(getType (link: this ^float x) :float)
(getType (link: this ^double x) :double)
(getType (link: this ^Object x) :object))
(defmacro pt (link: x)
`(.getType (PrimitiveTester.) ~x))
(defn with-defn ^double (link: ^double x)
(+ x 0.5))
(pt (with-defn 1.0)) ; => :double
(let (link: a (fn ^double [^double x) (+ x 0.5))]
(pt (a 0.1))) ; => :object
请参阅邮件列表上的讨论,了解更多关于所发生事情的信息和思考。
http://groups.google.com/group/clojure/browse_thread/thread/d83c8643a7c7d595?hl=en