clojureclojure-java-interop

How to type hint a float?


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


Solution

  • Try this (capitalized Float)

    (defn ff2a
      ^"[F" ; Return type hint for a float array
      [^Float f1 ^Float f2] ; Argument type hints
      (float-array [f1 f2]))
    

    In Java, every primitive type (like float, boolean, int) has a class counterpart (Float, Boolean, Integer).

    Since Only long and double primitives are supported, you'll need to use the class version of float.