<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged functional-interface</title>
<link>https://ask.clojure.org/index.php/tag/functional-interface</link>
<description>Powered by Question2Answer</description>
<item>
<title>Compiled functional interface adapters with 3 or more args silently truncate out-of-range numeric return values</title>
<link>https://ask.clojure.org/index.php/15310/compiled-functional-interface-adapters-silently-truncate</link>
<description>&lt;p&gt;Clojure 1.12.7, JDK 21 (same on master).&lt;/p&gt;
&lt;p&gt;A compiled call that adapts a Clojure function to a functional interface with 3 or more arguments and a primitive numeric return (&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;byte&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, or &lt;code&gt;long&lt;/code&gt;) silently truncates out-of-range return values.&lt;/p&gt;
&lt;p&gt;Expected: throw &lt;code&gt;ArithmeticException&lt;/code&gt; (for &lt;code&gt;int&lt;/code&gt;) or &lt;code&gt;IllegalArgumentException&lt;/code&gt; (for &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;byte&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;), matching 0 to 2 argument adapters and reflective calls.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(definterface ^{java.lang.FunctionalInterface true} SAM2I (^int f [a b]))
(definterface ^{java.lang.FunctionalInterface true} SAM3I (^int f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3S (^short f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3B (^byte f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3F (^float f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3L (^long f [a b c]))
(definterface Taker
  (^int t2i [^user.SAM2I s])
  (^int t3i [^user.SAM3I s])
  (^short t3s [^user.SAM3S s])
  (^byte t3b [^user.SAM3B s])
  (^float t3f [^user.SAM3F s])
  (^long t3l [^user.SAM3L s]))
(def tk (reify user.Taker
  (t2i [_ s] (.f s 1 2))
  (t3i [_ s] (.f s 1 2 3))
  (t3s [_ s] (.f s 1 2 3))
  (t3b [_ s] (.f s 1 2 3))
  (t3f [_ s] (.f s 1 2 3))
  (t3l [_ s] (.f s 1 2 3))))

(.t2i ^user.Taker tk (fn [a b] 4294967297))
;; =&amp;gt; ArithmeticException: integer overflow
(.t3i (identity tk) (fn [a b c] 4294967297))
;; =&amp;gt; ArithmeticException: integer overflow

(.t3i ^user.Taker tk (fn [a b c] 4294967297))
;; Actual: 1
;; Expected: ArithmeticException: integer overflow
(.t3s ^user.Taker tk (fn [a b c] 70000))
;; Actual: 4464
;; Expected: IllegalArgumentException
(.t3b ^user.Taker tk (fn [a b c] 300))
;; Actual: 44
;; Expected: IllegalArgumentException
(.t3f ^user.Taker tk (fn [a b c] 1e100))
;; Actual: ##Inf
;; Expected: IllegalArgumentException
(.t3l ^user.Taker tk (fn [a b c] 100000000000000000000N))
;; Actual: 7766279631452241920
;; Expected: IllegalArgumentException
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cause: for 3 to 10 parameters, &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/Compiler.java#L1712-L1716&quot;&gt;Compiler.maybeEmitFIAdapter&lt;/a&gt; picks an &lt;code&gt;Object&lt;/code&gt;-returning method from &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/FnInvokers.java#L750-L780&quot;&gt;FnInvokers&lt;/a&gt;, so &lt;code&gt;LambdaMetafactory&lt;/code&gt; unboxes without &lt;code&gt;RT.*Cast&lt;/code&gt; range checks. &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/Reflector.java#L578-L592&quot;&gt;Reflector.coerceAdapterReturn&lt;/a&gt; always runs those checks. Adding primitive-returning &lt;code&gt;FnInvokers&lt;/code&gt; methods for 3 to 10 &lt;code&gt;Object&lt;/code&gt; parameters would make all arities and both call paths consistent.&lt;/p&gt;
&lt;p&gt;This bug is reported at &lt;a rel=&quot;nofollow&quot; href=&quot;https://s.conao3.com/blog/2026/e4f733&quot;&gt;https://s.conao3.com/blog/2026/e4f733&lt;/a&gt;&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15310/compiled-functional-interface-adapters-silently-truncate</guid>
<pubDate>Thu, 03 Dec 2026 20:04:15 +0000</pubDate>
</item>
</channel>
</rss>