On Mon, Nov 22, 2010 at 2:29 PM, nicolas.o...@gmail.com <nicolas.o...@gmail.com> wrote: >> Type hints don't reduce flexibility AT ALL. >> >> user=> (defn t1 [x] (.length x)) >> #'user/t1 >> user=> (t1 "foo") >> 3 >> user=> (t1 'foo) >> #<CompilerException java.lang.IllegalArgumentException: No matching >> field found: length for class clojure.lang.Symbol >> (NO_SOURCE_FILE:141)> >> >> Since it calls a String method it doesn't work if you call it with a >> non-String. > > It prevents duck-typing: t1 works on any object having a length method. > t2 only works on String.
It prevents duck typing with direct interop. How common is that, though? Most usually if there's a Java method with sufficiently similar semantics as to be logically considered "the same method" on multiple classes, there's a common interface or superclass they all get it from and you can type hint to that. Duck typing internal to clojure, such as calling seq on Strings and Collections, still works. And you can make your own Clojure functions that duck type, e.g. (defn close! [x] (condp #(instance? %1 %2) x Stream (.close ^Stream x) JFrame (.dispose ^JFrame x) ResultSet (.close ^ResultSet x) URLConnection (.close ^URLConnection x))) and then call (close! foo) on various resources to close them. More clauses can be added as necessary. Or you can use defprotocol to make a Closeable protocol that you can then extend for anything that has .close, .dispose, or similar Java methods. (The type hints in the above defn prevent you from losing the benefits of type hinting by going through close. The caller of close! can type hint various things and also close various things, and even close a thing that might have different types at different times. You can also (doall (map close! seq-of-resources)) and similarly.) -- 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 Note that posts from new members are moderated - please be patient with your first post. 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