<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged type-hints</title>
<link>https://ask.clojure.org/index.php/tag/type-hints</link>
<description></description>
<item>
<title>Reflection warning when long hint on an argument</title>
<link>https://ask.clojure.org/index.php/15067/reflection-warning-when-long-hint-on-an-argument</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I noticed some reflection warning that goes away when &lt;em&gt;removing&lt;/em&gt; a hint from fn arguments. I would like to understand the reason for this.&lt;/p&gt;
&lt;p&gt;Example, loading this code&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(set! *warn-on-reflection* true)

(defn duration-of-seconds
  (^java.time.Duration [^long seconds]
   (java.time.Duration/ofSeconds seconds)))

(defn sleep
  [seconds]
  (Thread/sleep (duration-of-seconds seconds)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Raises a reflection warning:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;call to static method sleep on java.lang.Thread can't be resolved (argument types: java.lang.Object).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, just removing the &lt;code&gt;^long&lt;/code&gt; hint from the &lt;code&gt;duration-of-seconds&lt;/code&gt; fn argument, makes the reflection warning go away.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15067/reflection-warning-when-long-hint-on-an-argument</guid>
<pubDate>Fri, 01 May 2026 19:49:31 +0000</pubDate>
</item>
<item>
<title>Macro expansion lose type hints</title>
<link>https://ask.clojure.org/index.php/14740/macro-expansion-lose-type-hints</link>
<description>&lt;p&gt;The following snippet demonstrate how type hints when put before a macro expression are being lost.&lt;br&gt;
On the first expression, the compiler correctly complains about the wrong type hint while it gets accepted if you wrap the hinted expression on a macro. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clj 
Clojure 1.12.3

user=&amp;gt; (import 'java.util.Date)

user=&amp;gt; (.setHours (Date.) ^long (.getHours (Date.)))
Syntax error (UnsupportedOperationException) compiling fn* at (REPL:1:1).
Cannot coerce int to long, use a cast instead

user=&amp;gt; (.setHours (Date.) ^long (-&amp;gt; (.getHours (Date.))))
nil
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14740/macro-expansion-lose-type-hints</guid>
<pubDate>Mon, 03 Nov 2025 13:50:41 +0000</pubDate>
</item>
<item>
<title>Request: def should infer type tag where possible</title>
<link>https://ask.clojure.org/index.php/13872/request-def-should-infer-type-tag-where-possible</link>
<description>&lt;p&gt;Maybe there’s a good reason why it doesn’t, but it seems to me it would be great if def would automatically infer type hint:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [s &quot;abc&quot;]
  (.chars s)) ;; all fine, no warning

(def s &quot;abc&quot;)

(.chars s) ;; Reflection warning, null:21:0 - reference to field chars can't be resolved.

(meta #'s) ;; ↓ notice -- no tag
;; {:line 19,
;;  :column 0,
;;  :file &quot;NO_SOURCE_FILE&quot;,
;;  :name s,
;;  :ns #object[clojure.lang.Namespace 0x78068afb &quot;user&quot;]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Related jira: &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-2851&quot;&gt;https://clojure.atlassian.net/browse/CLJ-2851&lt;/a&gt;&lt;br&gt;
Related question: &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/9167/infer-hint-from-return-type-hint-function-call-initializes&quot;&gt;https://ask.clojure.org/index.php/9167/infer-hint-from-return-type-hint-function-call-initializes&lt;/a&gt;&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13872/request-def-should-infer-type-tag-where-possible</guid>
<pubDate>Fri, 03 May 2024 20:01:21 +0000</pubDate>
</item>
<item>
<title>Why can't I type hint these floats?</title>
<link>https://ask.clojure.org/index.php/13743/why-cant-i-type-hint-these-floats</link>
<description>&lt;p&gt;I want to ensure I'm using unboxed and unreflected arithmetic on arrays of floats, but I can't get it to compile.&lt;/p&gt;
&lt;p&gt;How can I type hint a float in a function like this?&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
(defn ff2a&lt;/p&gt;
&lt;p&gt;  ^&quot;[F&quot; ; Return type hint for a float array&lt;/p&gt;
&lt;p&gt;  [^float f1 ^float f2] ; Argument type hints&lt;/p&gt;
&lt;p&gt;  (float-array [f1 f2]))&lt;br&gt;
&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I get this error when I try to load it into the REPL:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Syntax error (IllegalArgumentException) compiling fn* [...] Only long and double primitives are supported&lt;/code&gt;&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13743/why-cant-i-type-hint-these-floats</guid>
<pubDate>Thu, 15 Feb 2024 18:48:26 +0000</pubDate>
</item>
<item>
<title>let binding does not infer primitive type of type hinted global var</title>
<link>https://ask.clojure.org/index.php/12599/let-binding-does-not-infer-primitive-type-type-hinted-global</link>
<description>&lt;p&gt;When you type hint a var such as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def ^{:tag 'long} k 100)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And refer to it in a &lt;code&gt;let&lt;/code&gt; binding:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [i k]
  (+ i 10))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The binding is unable to properly infer that it is a primitive type as it was hinted:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(set! *unchecked-math* :warn-on-boxed)
(def ^{:tag 'long} k 100)
(let [i k]
  (+ i 10))
;;=&amp;gt; Boxed math warning, unchecked_add(java.lang.Object,long).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It looks like it might be caused by a bug in the Compiler where for some reason, the expression of the var k is marked as not having a java class, even though it has one:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro inspect-local []
  (println
   (into {}
         (map (fn[[k v]]
                [k {:tag (.-tag v)
                    :class (.getJavaClass v)
                    :primitive? (.isPrimitive (.getJavaClass v))
                    :has-java-class? (.hasJavaClass v)}]))
         &amp;amp;env)))

(let [i k]
  (inspect-local)
  (+ i 10))

;;=&amp;gt; {i {:tag nil, :class long, :primitive? true, :has-java-class? false}}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, &lt;code&gt;.hasJavaClass&lt;/code&gt; returns false, but &lt;code&gt;.getJavaClass&lt;/code&gt; returns the &lt;code&gt;Long/TYPE&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;I suspect that the compiler checks for &lt;code&gt;.hasJavaClass&lt;/code&gt; before calling &lt;code&gt;.getJavaClass&lt;/code&gt;, and since &lt;code&gt;.hasJavaClass&lt;/code&gt; is erroneously false here, it would cause the compiler to think it isn't, and that the type can't be inferred.&lt;/p&gt;
&lt;p&gt;I'm not too sure where in the Compiler this is, but I found the following: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L1341&quot;&gt;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L1341&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Where we see the compiler is first checking for &lt;code&gt;.hasJavaClass&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; if(e instanceof MaybePrimitiveExpr &amp;amp;&amp;amp; e.hasJavaClass() &amp;amp;&amp;amp; ((MaybePrimitiveExpr)e).canEmitPrimitive())
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12599/let-binding-does-not-infer-primitive-type-type-hinted-global</guid>
<pubDate>Mon, 30 Jan 2023 03:23:21 +0000</pubDate>
</item>
<item>
<title>Macro symbols in &amp;env don't get updated when shadowed, still contain the outer symbol instead.</title>
<link>https://ask.clojure.org/index.php/12598/macro-symbols-updated-shadowed-still-contain-symbol-instead</link>
<description>&lt;pre&gt;&lt;code&gt;(defmacro inspect
  [&amp;amp; body]
  (println (keys &amp;amp;env) (map meta (keys &amp;amp;env)))
  `(do ~@body))

(let [^:1 a 1]
  (let [^:2 a 2]
    (let [^:3 a 3]
      (inspect &quot;hello&quot;))))

;;=&amp;gt; (a) ({:1 true})
&quot;hello&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;&amp;amp;env&lt;/code&gt; form returns a map of &lt;code&gt;{sym LocalBinding}&lt;/code&gt; and the symbols on the keys of the map are supposed to be the local symbols, but as we see in the above code, when a local symbol is shadowing another, it seems the map does not contain the shadowing symbol, but instead contains the shadowed symbol. We can see that because it is returning the metadata of the outermost symbol, and not the inner one that is shadowing it.&lt;/p&gt;
&lt;p&gt;In my opinion, this is a bug, that said, you can still get at the actual shadowing symbol by calling &lt;code&gt;.-sym&lt;/code&gt; on the LocalBinding, in which case you get the inner shadowing symbol and it will have the right metadata.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12598/macro-symbols-updated-shadowed-still-contain-symbol-instead</guid>
<pubDate>Mon, 30 Jan 2023 03:13:31 +0000</pubDate>
</item>
<item>
<title>into-array should not need type hints</title>
<link>https://ask.clojure.org/index.php/10609/into-array-should-not-need-type-hints</link>
<description>&lt;p&gt;Currently into-array's return value needs type hinting in addition to having to specify the target class in the first place. Type hinting array types isn't exactly trivial either (with the weird and extremely verbose ^&quot;[Lorg.clojure.MyClass;&quot; syntax).&lt;/p&gt;
&lt;p&gt;It would be really nice if the compiler could deal with that for the user.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10609/into-array-should-not-need-type-hints</guid>
<pubDate>Sat, 15 May 2021 16:49:15 +0000</pubDate>
</item>
<item>
<title>Type hints getting ignored, reverting to java.lang.Object</title>
<link>https://ask.clojure.org/index.php/10555/type-hints-getting-ignored-reverting-to-java-lang-object</link>
<description>&lt;pre&gt;&lt;code&gt;  (deftype l [^java.util.List x])
  (use '[clojure.reflect :as r])
  (:members (r/reflect (l. `())))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Despite the type-hints of &lt;code&gt;java.util.List&lt;/code&gt;, on reflection x seems to always be an Object&lt;/p&gt;
&lt;p&gt;Have been specifically trying to get an optaplanner example working in 100% clojure &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/joinr/optaplanner-clj/tree/163d326a6433b88ca9811e33d30758ceff42b91f&quot;&gt;https://github.com/joinr/optaplanner-clj/tree/163d326a6433b88ca9811e33d30758ceff42b91f&lt;/a&gt; . The code is evaluating but this error keeps occurring on running &lt;code&gt;(solve (-&amp;gt;problem))&lt;/code&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;ol&gt;
&lt;li&gt;Unhandled java.lang.IllegalStateException    The solutionClass (class optaplanner_clj.data.TimeTable) has a &lt;br&gt;
PlanningEntityCollectionProperty annotated member (public final &lt;br&gt;
java.lang.Object optaplanner_clj.data.TimeTable.lessonList) that does&lt;br&gt;
not    return a Collection or an array.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10555/type-hints-getting-ignored-reverting-to-java-lang-object</guid>
<pubDate>Sun, 02 May 2021 01:56:00 +0000</pubDate>
</item>
<item>
<title>Can I use spec as type hint?</title>
<link>https://ask.clojure.org/index.php/10464/can-i-use-spec-as-type-hint</link>
<description>&lt;p&gt;One cool feature of schema is that it works well with type hint,&lt;br&gt;
So I can use it for validation, compiler optimization and documentation directly on the function. I'd like to know if the same could be done using specs and if possible, a real example. Thanks!&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10464/can-i-use-spec-as-type-hint</guid>
<pubDate>Sun, 11 Apr 2021 07:30:38 +0000</pubDate>
</item>
<item>
<title>Can the clojure.tools.trace reflection warnings be addressed?</title>
<link>https://ask.clojure.org/index.php/10171/can-the-clojure-tools-trace-reflection-warnings-addressed</link>
<description>&lt;p&gt;I frequently see these as part of my daily workflow - which is slightly inconvenient:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Reflection warning, clojure/tools/trace.clj:231:54 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:242:56 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:244:49 - call to java.lang.Exception ctor can't be resolved.
Reflection warning, clojure/tools/trace.clj:244:69 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:256:56 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:259:49 - call to java.lang.Throwable ctor can't be resolved.
Reflection warning, clojure/tools/trace.clj:259:69 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:275:56 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
Reflection warning, clojure/tools/trace.clj:276:68 - call to method setStackTrace on java.lang.Object can't be resolved (no such method).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Checking out the tools.trace code @ master, these seem tractable.&lt;/p&gt;
&lt;p&gt;Could these reflection warnings be avoided?&lt;/p&gt;
</description>
<category>tools.trace</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10171/can-the-clojure-tools-trace-reflection-warnings-addressed</guid>
<pubDate>Thu, 11 Feb 2021 18:39:41 +0000</pubDate>
</item>
<item>
<title>Typehint `clojure.spec.alpha/explain-str`</title>
<link>https://ask.clojure.org/index.php/9985/typehint-clojure-spec-alpha-explain-str</link>
<description>&lt;p&gt;Type hinting &lt;code&gt;clojure.spec.alpha/explain-str&lt;/code&gt; would help in instances like the following,&lt;br&gt;
&lt;code&gt;(throw (IllegalArgumentException. (s/explain-str ::some-spec value)))	               &lt;/code&gt;&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9985/typehint-clojure-spec-alpha-explain-str</guid>
<pubDate>Tue, 29 Dec 2020 18:19:38 +0000</pubDate>
</item>
<item>
<title>Reflection warnings in clojure.data.xml</title>
<link>https://ask.clojure.org/index.php/9874/reflection-warnings-in-clojure-data-xml</link>
<description>&lt;p&gt;The latest-on-Maven (0.2.0-alpha6) clojure.data.xml incurs reflection warnings.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clj
Clojure 1.10.1
user=&amp;gt; (set! *warn-on-reflection* true)
true
user=&amp;gt; (require '[clojure.data.xml :as dx])
Reflection warning, clojure/data/xml/jvm/name.clj:35:1 - call to static method decode on java.net.URLDecoder can't be resolved (argument types: unknown, java.lang.String).
Reflection warning, clojure/data/xml/jvm/name.clj:38:1 - call to static method encode on java.net.URLEncoder can't be resolved (argument types: unknown, java.lang.String).
nil
user=&amp;gt; (slurp &quot;deps.edn&quot;)
&quot;{:deps\n {org.clojure/data.xml {:mvn/version \&quot;0.2.0-alpha6\&quot;}}}&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There have been some updates in git, but they have not altered &lt;code&gt;jvm/name.clj&lt;/code&gt;, so the issue might still be current.&lt;/p&gt;
</description>
<category>data.xml</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9874/reflection-warnings-in-clojure-data-xml</guid>
<pubDate>Thu, 26 Nov 2020 15:04:41 +0000</pubDate>
</item>
<item>
<title>defrecord could type hint the record constructor functions</title>
<link>https://ask.clojure.org/index.php/9168/defrecord-could-type-hint-the-record-constructor-functions</link>
<description>&lt;p&gt;Here's some code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(set! *warn-on-reflection* true)
(defrecord Foo [bar])
(.-bar (-&amp;gt;Foo &quot;bar&quot;))

;;&amp;gt; Reflection warning, NO_SOURCE_PATH:1:1 - reference to field bar can't be resolved.
;;=&amp;gt; &quot;bar&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, the generated &lt;code&gt;-&amp;gt;Foo&lt;/code&gt; function to construct record of type &lt;code&gt;Foo&lt;/code&gt; is not type hinted, and thus Clojure will use reflection when doing interop calls over it.&lt;/p&gt;
&lt;p&gt;It would be nice if Clojure would generate a type hinted constructor instead.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9168/defrecord-could-type-hint-the-record-constructor-functions</guid>
<pubDate>Fri, 20 Mar 2020 00:17:49 +0000</pubDate>
</item>
<item>
<title>Infer Var type hint in a def from the return type hint of the function call that initializes it</title>
<link>https://ask.clojure.org/index.php/9167/infer-hint-from-return-type-hint-function-call-initializes</link>
<description>&lt;p&gt;Here's an example code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(set! *warn-on-reflection* true)
(defn hello ^String []
  &quot;hello&quot;)
(def a (hello))
(.replace a \e \a)

;;&amp;gt; Reflection warning, NO_SOURCE_PATH:1:1 - call to method replace can't be resolved (target class is unknown).
;;=&amp;gt; &quot;hallo&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, Clojure doesn't seem to be able to infer the type of the Var &lt;code&gt;a&lt;/code&gt;, even though the function &lt;code&gt;(hello)&lt;/code&gt; had its return type hinted.&lt;/p&gt;
&lt;p&gt;It would be great if Clojure could infer this. Otherwise, as of now, the user must provide an explicit type hint for it.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9167/infer-hint-from-return-type-hint-function-call-initializes</guid>
<pubDate>Fri, 20 Mar 2020 00:13:39 +0000</pubDate>
</item>
<item>
<title>Is there a way to type hint derefs?</title>
<link>https://ask.clojure.org/index.php/8873/is-there-a-way-to-type-hint-derefs</link>
<description>&lt;p&gt;There are cases where I have either a delay or a future returning a Java object, in those cases, it seems you can only type hint the returned value of deref from the call site.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(.reflectionAvoided ^SomeObj @some-delayed-java-object)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there a way to type hint it at the definition site? If not, would this be a logical feature request?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def some-delayed-java-object
  (delay (SomeObj.)))

(.someReflectionNeedingMethod @some-delayed-java-object)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank You&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8873/is-there-a-way-to-type-hint-derefs</guid>
<pubDate>Thu, 21 Nov 2019 02:00:05 +0000</pubDate>
</item>
</channel>
</rss>