When and where do you put type annotations in Clojure code? Obviously when performance counts. But are there rules by which you can live of when (only when doing Java Interop?) and where to add them (function definition arguments)?
When and where do you put type annotations in Clojure code? Obviously when performance
Share
The overriding reason that I"m aware of is performance. Type hinting in clojure removes reflection, which can slow down performance. So I would put type hints in functions which I’ve measured to be performance critical.
There is a description in the java interop section at clojure.org. Among the remarks:
You can always turn on the warn on reflection flag to see where reflection is being called, and consequently, where type hints might help.
EDIT:
Regarding your question "Only for java interop?" –no. Type hints will be useful even in a pure clojure application. The issue of reflection slowing down performance is a fact of life on the JVM, and is an issue that dynamic languages have to deal with in general.
Regarding where type hints can be placed, from clojure.org: