Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Compiler by

You can add type hints to the argument vector(s) of defn to declare the return type of a function like so (with warn-on-reflection being true):

`
user> (defn foo ^String [s] s)

'user/foo

user> (.substring (foo "hallo") 1 2)
"a"
`

But sadly, the same doesn't work with fn and letfn:

`
user> (.substring ((fn ^String [s] s) "hallo") 1 2)
Reflection warning, NO_SOURCE_PATH:1:1 - call to substring can't be resolved.
"a"
user> (letfn [(foo ^String [s] s)]

(.substring (foo "hallo") 1 2))

Reflection warning, NO_SOURCE_PATH:2:7 - call to substring can't be resolved.
"a"
`

I don't see why this feature is available to defn but not to fn and letfn. I even consider it a kind of defect, because anything else including :pre/:post are also supported by the latter two, so the support for hinting the return type should be there simply because of analogy.

1 Answer

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1231 (reported by tsdh)
...