Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Java Interop by

I want to ensure I'm using unboxed and unreflected arithmetic on arrays of floats, but I can't get it to compile.

How can I type hint a float in a function like this?

`
(defn ff2a

^"[F" ; Return type hint for a float array

[^float f1 ^float f2] ; Argument type hints

(float-array [f1 f2]))
`

I get this error when I try to load it into the REPL:

Syntax error (IllegalArgumentException) compiling fn* [...] Only long and double primitives are supported

1 Answer

+1 vote
by

Copying my answer from SO below.

You can't. The error is quite clear. Even this fails:

user=> (defn f [^float x])
Syntax error (IllegalArgumentException) compiling fn* at (REPL:1:1).
Only long and double primitives are supported

You can still use unboxed doubles. If that doesn't work for you, it might be better to implement this particular part in Java.

by
Well darn. I can't use Clojure for my project. I require dense float arrays. Thank you for the quick answer.
by
Having to use float arrays does not equate to type hinting every primitive float. Chances are, you can still use Clojure and have a comparable performance to what you'd have in Java. Especially given things like `tech.ml.dataset`.
by
float arrays are fine, you just can't pass primitive floats around (you will need doubles in this case). Clojure makes opinionated preference of doubles and longs over floats and integers. Arrays are objects though, that's orthogonal to this.

user=> (defn ff2a ^floats [^double f1 ^double f2] (float-array [f1 f2]))
#'user/ff2a
user=> (ff2a 1.2 3.4)
#object["[F" 0x50f40653 "[F@50f40653"]
user=> (seq *1)
(1.2 3.4)
...