Skip to content

Commit 07a6a5c

Browse files
committed
add faq entry
1 parent 7e85063 commit 07a6a5c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

content/guides/faq.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ Use a $ to separate outer from inner class name. For example: `java.util.Map$Ent
294294

295295
Primitive types can be found as the static TYPE field on the boxed class, for example: `Integer/TYPE`.
296296

297+
[[return_type_hint]]
298+
**<<faq#return_type_hint,How do you type hint a method return type?>>**
299+
300+
Return <<xref/../../../reference/java_interop#typehints,type hints>> can sometimes be useful to flow Java type information out of functions. To provide a return type hint, placed it on the function parameter vector:
301+
302+
[source,clojure]
303+
----
304+
(defn new-file ^java.io.File [path] (java.io.File. path))
305+
----
306+
307+
Note that type hints can also be placed on the var itself, but placing it on the parmaeter vector is preferred for two reasons:
308+
309+
1. Var type hints are evaluated and for special primitive or array type hints like `^long` or `^longs`, these happen to also name functions. When the var type hints are evaluated they become function objects, which are invalid type hints and ignored.
310+
2. A multi-arity function may specify different return type hints for different arities (although this is not common).
311+
297312
[[varargs]]
298313
**<<faq#varargs,How do you invoke a Java method with a vararg signature?>>**
299314

0 commit comments

Comments
 (0)