<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged type</title>
<link>https://ask.clojure.org/index.php/tag/type</link>
<description>Powered by Question2Answer</description>
<item>
<title>Type inference in defn?</title>
<link>https://ask.clojure.org/index.php/14027/type-inference-in-defn</link>
<description>&lt;pre&gt;&lt;code&gt;(ns graalvm-list-issue.main
  (:require
   [clojure.java.io :as io])
  (:gen-class))

(defn- home-path []
  (System/getProperty &quot;user.home&quot;))

(defn- local-dir []
  (io/file (home-path) &quot;.local&quot;))

(defn -main [&amp;amp; _args]
  (println (seq (.list (io/file (home-path) &quot;.local&quot;))))
  (println (seq (.list (local-dir)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Full code is here: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/conao3/clojure-graalvm-list-issue&quot;&gt;https://github.com/conao3/clojure-graalvm-list-issue&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The first &lt;code&gt;println&lt;/code&gt; line has no reflection, the second &lt;code&gt;println&lt;/code&gt; line has reflection, and an error occurs during execution in GraalVM.&lt;br&gt;
I see no difference between the two calls, and since it is obvious that it is io/file as the return value of &lt;code&gt;local-dir&lt;/code&gt;, it should be type-inferred as java.io.File. What do you think?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Investivate Clojure source code:&lt;/p&gt;
&lt;p&gt;Key function is &lt;code&gt;hasJavaClass&lt;/code&gt; and &lt;code&gt;getJavaClass&lt;/code&gt;.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L359-L361&quot;&gt;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L359-L361&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;FnExpr: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L4450-L4454&quot;&gt;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L4450-L4454&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;InvokeExpr: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L4286-L4290&quot;&gt;https://github.com/clojure/clojure/blob/8c8e4f6ef21f3d0f59deb60cdc7e1b584f596d59/src/jvm/clojure/lang/Compiler.java#L4286-L4290&lt;/a&gt;&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14027/type-inference-in-defn</guid>
<pubDate>Mon, 22 Jul 2024 01:43:33 +0000</pubDate>
</item>
<item>
<title>Do primitive type hints in fn arguments turns the return value as an Object?</title>
<link>https://ask.clojure.org/index.php/13501/primitive-type-hints-arguments-turns-the-return-value-object</link>
<description>&lt;p&gt;Hi everyone,&lt;br&gt;
I was playing around with java-interop and found this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(set! *warn-on-reflection* true)

(import [java.util.concurrent ScheduledExecutorService Executors ScheduledFuture TimeUnit])

(defonce ^:private ^ScheduledExecutorService executor (Executors/newScheduledThreadPool 1))

(defn set-timeout-primitive ^ScheduledFuture [^long ms ^Callable f]
  (.schedule executor f ms TimeUnit/MILLISECONDS))

(defn set-timeout-object ^ScheduledFuture [^Long ms ^Callable f]
  (.schedule executor f ms TimeUnit/MILLISECONDS))

(.get (set-timeout-primitive 2000 (fn [] (println :done) :done))) ; Reflection warning, ... - reference to field get on java.lang.Object can&#039;t be resolved.
(.get (set-timeout-object 2000 (fn [] (println :done) :done))) ; No warnings
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The reason for this reflection warning (thanks for &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C03S1KBA2/p1701334231189649&quot;&gt;explaining&lt;/a&gt;) seems to be that when the compiler looks for the matching interface it finds this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static public interface OLOO{Object invokePrim(Object arg0, long arg1, Object arg2);}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So the return value becomes an &lt;code&gt;Object&lt;/code&gt; and not a &lt;code&gt;ScheduledFuture&lt;/code&gt;. But this doesn&#039;t happen when using a &lt;code&gt;^Long&lt;/code&gt; type hint instead of a &lt;code&gt;^long&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Could this be improved? Thank you!&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13501/primitive-type-hints-arguments-turns-the-return-value-object</guid>
<pubDate>Fri, 01 Dec 2023 18:42:52 +0000</pubDate>
</item>
<item>
<title>Why doesn&#039;t `type` in CLJS check `:type` on its arguments meta like CLJ does?</title>
<link>https://ask.clojure.org/index.php/13095/why-doesnt-type-cljs-check-type-its-arguments-meta-like-does</link>
<description>&lt;p&gt;In CLJ a check for &lt;code&gt;:type&lt;/code&gt; on the meta happens and then falls back to returning the constructor class. In CLJS only the constructor class is returned. It would be nice to have these consistent for usage with multimethods etc. &lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13095/why-doesnt-type-cljs-check-type-its-arguments-meta-like-does</guid>
<pubDate>Sun, 23 Jul 2023 14:56:02 +0000</pubDate>
</item>
<item>
<title>how to check whether an object is of a certain type</title>
<link>https://ask.clojure.org/index.php/9205/how-to-check-whether-an-object-is-of-a-certain-type</link>
<description>&lt;p&gt;Looking at the documentation for &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/type&quot;&gt;type&lt;/a&gt; I see how to find a type of an object.  And in the see-also section it talks about the function &lt;code&gt;instance?&lt;/code&gt; which tests whether an object is a member of a designated class.  &lt;/p&gt;
&lt;p&gt;Is the documentation using the word class and type interchangeably here?&lt;br&gt;
What is the correct way to check type membership, assuming I have a type designator compatible with &lt;code&gt;isa?&lt;/code&gt; ?&lt;/p&gt;
</description>
<category>Records and Types</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9205/how-to-check-whether-an-object-is-of-a-certain-type</guid>
<pubDate>Wed, 01 Apr 2020 09:10:09 +0000</pubDate>
</item>
</channel>
</rss>