4 ms·
I'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
by kenko 12y ago
I'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.