Search code examples
clojurestatic-typing

Clojure static typing


I know that this may sound like blasphemy to Lisp aficionados (and other lovers of dynamic languages), but how difficult would it be to enhance the Clojure compiler to support static (compile-time) type checking?

Setting aside the arguments for and against static and dynamic typing, is this possible (not "is this advisable")?

I was thinking that adding a new reader macro to force a compile-time type (an enhanced version of the #^ macro) and adding the type information to the symbol table would allow the compiler to flag places where a variables was misused. For example, in the following code, I would expect a compile-time error (#* is the "compile-time" type macro):

(defn get-length [#*String s] (.length s))
(defn test-get-length [] (get-length 2.0))

The #^ macro could even be reused with a global variable (*compile-time-type-checking*) to force the compiler the do the checks.

Any thoughts on the feasibility?


Solution

  • It's certainly possible. The compiler already does some static type checking around primitive argument types in the 1.3 development branch.