Is it possible to force Clojure into using primitive types instead of their boxed versions?

Viewed 174

I have a following sample of code:

(type (apply / [5.0 0]))

It throws an unexpected error - "Division By Zero" (expected behavior: return Inf)

Probably, it happens due to auto-boxing - can it be prevented?

2 Answers

Assuming you just want to use a two argument division on doubles, the following might work.

user=> (apply (fn d2div [n d] (double (/ (double n) (double d)))) [5.0 0.0])
##Inf

If you go down this path, check out clj-cmmons/primitive-math.

How about

user=> (defmacro myapply [func args] `(~func ~@args))
#'user/myapply
user=> (myapply / [5.0 0])
##Inf
user=> (type (myapply / [5.0 0]))
java.lang.Double

I didn't know that apply behaves in such a different manner in Clojure.

Related