3 ms·
This is an example of dispatching on return type in Clojure - which is similar to Common Lisp. http://stackoverflow.com/questions/22740178/clojure-dispatch-on-
by juliangamble 12y ago
This is an example of dispatching on return type in Clojure - which is similar to Common Lisp.
http://stackoverflow.com/questions/22740178/clojure-dispatch-on-return-type-as-expressive-as-haskell-typeclasses http://stackoverflow.com/questions/22740178/clojure-dispatch...
(The Haskell purists point out that the dispatch isn't determined at compile time - form your own opinion about whether they have a point).
- kenko 12y agoI'm not a Haskell purist, but this isn't really dispatch on return type, this is manual inspection of manually provided metadata that just happens, in this case, to be (intended to be) a return type. The return type isn't enforced, incidentally: user> (defn ^String my-function [^Integer foo] (apply concat (repeat foo "x"))) #'user/my-function user> (my-function 3) (\x \x \x) Whoops, I meant (apply str ...). You also can't use the metadata on a function introduced by (defn ^ReturnType ...) in more or less any useful way, if you don't have a reference to the var itself. If you pass it to a function, you basically can't recover it: user> (defn do-something-interesting-with-return-type [f] (println "here's my metadata:" (meta f))) #'user/do-something-interesting-with-return-type user> (do-something-interesting-with-return-type my-function) here's my metadata: nil nil Gone! You can tell that something fishy is going on in the SO answer because the author has to pass in the function by name, #'return-string.