I like that Clojure is a dynamically typed language. Even in dynamic languages it is possible to find out a lot more about the code itself than one may think on a first glance. Clojure already supports type hints that can make code run faster. But what if we could add a soft layer of static typing into the compiler, by making promises to it, which can then be enforced? For example, we could promise that foo will always be a string: (def+ foo "Hello Clojure")
Same for functions, about its parameters and return values. Note that I am not proposing a concrete syntax here, this is just a sketch, so we can talk about it: (defn reverse "Returns a seq of the items in coll in reverse order. Not lazy." [#^Collection coll] (reduce conj () coll)) For all functions in the core.clj which don’t have side effects we could make this promise. In that case the compiler could check if foo is ever used in a position where not a string is expected. And it could check if the return value of a function is an argument to another function, and then see if those match: (+ (count foo) 15) ==> 28 (dec foo) ==> compile time error, can't subtract 1 from a String. (count (count foo)) ==> compile time error, as count returns a number, not a collection. This would keep Clojure still extremly dynamic. One exception is: functions and vars for which we gave promises can’t be changed anymore during runtime. But these compiler checks would only be made when in a form there only occur vars/functions which all made promises about their types. These types can be inferred by Clojure. For example in (def+ foo "Hi") we see that foo refers to a string. As Clojure is mostly a functional language anyway it will not cause much harm that foo can’t be changed anymore. In principle each var that was def’ed should be seen as a constant. As long we are debugging our code it can of course make sense to change those vars. Anyway, with this proposal a good bit of code can be checked for type correctness and there is another positive aspect: the Clojure editors could make use of it. When we write (count<space> the editor will not offer us auto-completion for vars that we promised to be numbers or microwave objects. This feature would allow the IDE to do more refactoring and more code completion at edit time. I am proposing a complete optional mode. Into the metadata one could add a promise and state the explicit types, or add just an empty promise and have the Clojure compiler then to infer the correct times (it could give warnings if this were not possible). This way only people who like these features could use them. Even if the core.clj and the whole load of clojure-contrib changed in a way to fully support soft static typing, then still a user can decides to never put promises into his code, and so he wouldn’t even notice that there is some (soft) mechanism for static typing. Opinions? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---