What does “^:static” do in Clojure?

2019-04-03 02:08发布

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?

3条回答
地球回转人心会变
2楼-- · 2019-04-03 02:27

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

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.

查看更多
萌系小妹纸
3楼-- · 2019-04-03 02:30

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.

查看更多
混吃等死
4楼-- · 2019-04-03 02:47

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

查看更多
登录 后发表回答