I write sometimes java methods, especially for primitives/arrays operations and I am always stuck about how to use type hints under Clojure 1.8. I saw some threads but they are (maybe ?) outdated considering they have been posted more than 2 years ago.
I will give a basic example to illustre my point (I know this example is a bit pointless). Here i want to sum two double and to return a double.
Here is a Java method :
public static double add (double a, double b) {
return a + b;
}
Then I would like to have a Clojure wrapper :
Version 1
(defn d+ ^double
[^double a ^double b]
(Doubles/add a b))
Version 2
(defn d+ ^double
[^double a ^double b]
(Doubles/add ^double a ^double b))
Version 3
(defn d+ ^double
[^double a ^double b]
(Doubles/add (double a) (double b)))
I do not know where to put type hints and how to put them. I have the impression that (double x) is less efficient since it is a function (maybe I am wrong ?).
Then what is the difference between giving hints inside body function or outside ?
Or maybe these hints are not necessary since there is only one method in the Java class ? I do not see the logic so generally I use version 1 or 3 (more is better ?).
Note that for this example, Clojure + is always faster
*warn-on-reflection*flag would be helpful for this kind of explorations. Just set it to true, and the compiler would signal the case where it can't select needed method overload and needs a hint.(set! *unchecked-math* :warn-on-boxed)at the begging of you clojure file.