Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "^:static" do in Clojure?

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?

like image 405
mikera Avatar asked Sep 26 '11 08:09

mikera


2 Answers

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

like image 29
bmillare Avatar answered Oct 20 '22 18:10

bmillare


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.

like image 86
Arthur Ulfeldt Avatar answered Oct 20 '22 19:10

Arthur Ulfeldt