in Clojure 1.2.x you can only force local variables, and they cannot cross function calls. Starting with Clojure 1.3.0, Clojure can use primitive numbers to call functions, but not through higher-order functions, such as map .
if you are using Clojure 1.3.0+ then you can accomplish this using tooltips like
as with any Clojure optimization problem, the first step is to enable (set! *warn-on-reflection* true) , then add type hints until it no longer complains.
user=> (set! *warn-on-reflection* true) true user=> (defn binary-search [coll coll-size target] (let [cnt (dec coll-size)] (loop [low-idx 0 high-idx cnt] (if (> low-idx high-idx) nil (let [mid-idx (quot (+ low-idx high-idx) 2) mid-val (coll mid-idx)] (cond (= mid-val target) mid-idx (< mid-val target) (recur (inc mid-idx) high-idx) (> mid-val target) (recur low-idx (dec mid-idx)) )))))) NO_SOURCE_FILE:23 recur arg for primitive local: low_idx is not matching primitive, had: Object, needed: long Auto-boxing loop arg: low-idx #'user/binary-search user=>
to remove this you can enter a column size argument hint
(defn binary-search [coll ^long coll-size target] (let [cnt (dec coll-size)] (loop [low-idx 0 high-idx cnt] (if (> low-idx high-idx) nil (let [mid-idx (quot (+ low-idx high-idx) 2) mid-val (coll mid-idx)] (cond (= mid-val target) mid-idx (< mid-val target) (recur (inc mid-idx) high-idx) (> mid-val target) (recur low-idx (dec mid-idx)) ))))))
for obvious reasons, itβs hard to connect the autobox on line 10 to the coll-size parameter, since it goes through cnt , then high-idx , then mid-ixd , etc., so I usually approach these problems with type-hinting everything until I find one that causes the warnings to go away, and then removes the prompts until they go away.
source share