The argument against type hinting in Clojure

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

Intersting:

But it doesn’t hurt, does it?
So one might think: “It doesn’t help, but it doesn’t hurt either, so I sprinkle hints all over the place to give myself some info on what this function returns.” And indeed this thinking is wrong.
By now it should be clear that type hints are a low-level construct. Using them in the above mentioned way over specifies the types the functions take and return. You basically lock the code which could in theory be host independent into one platform.
Take for example str. It is hinted to return a String. Is this wrong? No. It uses a host dependent way to efficiently create strings. So you need a custom str version a on different host anyway, which may then be hinted with a different type.
Take for example the following str-cat function:
(defn ^String str-cat
[& xs]
(apply str xs))
Is the type hint wrong? Yes! This function would work on any host in the same way. But by needlessly hinting it, we lock it into the JVM.
Upshot
Only add type hints when you really need them. That is in host interop call sites. And even then you should check, that your really need them.
Sometimes you may even choose to go with the reflective call. But beware the library author. Others will have to live with it!
*warn-on-reflection* is your friend.
Just adding type hints doesn’t turn Clojure magically into a statically typed language. Type hints are not Typed Clojure.

Post external references

  1. 1
    https://kotka.de/blog/2012/06/Did_you_know_IX.html
Source