What does “^:static” do in Clojure?

2019-04-03 02:09发布

问题:

I've seen the ^:static metadata on quite a few function in the Clojure core.clj source code, e.g. in the definition of seq?:

(def
 ^{:arglists '([x])
   :doc "Return true if x implements ISeq"
   :added "1.0"
   :static true}
 seq? (fn ^:static seq? [x] (instance? clojure.lang.ISeq x)))

What precisely does this metadata do, and why it it used so frequently throughout core.clj?

回答1:

In the development of Clojure 1.3 Rich wanted to add the ability for functions to return types other than Object. This would allow native math operators to be used without having to cram everything into one function.

The original implementation required functions that supported this to be marked :static. this meta data caused the compiler to produce two versions to the function, one that returned Object and one that returned that specific type. in cases where the compiler determined that the types would always match the more specific version would be used.

This was later made fully automatic so you don't need to add this anymore.



回答2:

According to the Google Groups thread “Type hinting inconsistencies in 1.3.0”, it’s a no-op.

^:static has been a no-op for a while AFAIK, made unnecessary after changes to vars a while back.

— a May 2011 post by Chas Emerick



回答3:

Seems it's a new metadata attribute in clojure 1.3. And you can compare the source between 1.3 and 1.2:

  • http://clojuredocs.org/clojure_core/clojure.core/seq_q
  • http://clojuredocs.org/clojure_core/1.2.0/clojure.core/seq_q

So I think it has something to do with ^:dynamic which indicates whether the var is allowed for dynamic binding. Just my guess. Not sure until I see document about this attribute.