clojurejava-interopgen-class

Clojure's :gen-class and double arrays


I am attempting to :gen-class a fn which takes a 2D array of Doubles as input. I have already seen the post and solution here concerning a similar topic, but I am still unable to produce a working solution.

(ns gui.heatmap
  (:gen-class
    :name gui.Heatmap
    :methods [[heat-map2 ["[[D"] org.jfree.chart.JFreeChart]]))

(defn foo [dbl-array]
  ...)

I use the "[[D" based on using type on my input. This compiles fine into a .class file.

Now, when I move to another .clj file, I have the following.

(ns ...
  (import (gui.Heatmap)))

(defn bar [args]
  ...
  (.foo
    (into-array
      (vector
        (double-array <list of numbers>)
        (double-array <list of numbers>)
        (double-array <list of numbers>)))))

When I call bar from the repl, I get the following error:

java.lang.IllegalArgumentException: No matching field found: heat_map2 for class [[D

Any thoughts?


Solution

  • You are missing the object. (.foo (into-array ...)) vs (.foo (Heatmap.) (into-array...))

    Note, you should also require your gui.Heatmap namespace. Otherwise you can get into trouble if the ... namespace is compiled before gui.Heatmap. Then the import fails, because the class is not generated, yet. Adding the require will resolve this problem.

    Edit:

    To clarify things.

    (ns gui.heatmap
      (:gen-class
        :name gui.Heatmap
        :methods [[heat-map2 ["[[D"] org.jfree.chart.JFreeChart]]))
    
    (defn -foo [dbl-array]
      ...)
    
    (ns ...
      (import gui.Heatmap))
    
    (defn bar [args]
      ...
      (.foo
        (Heatmap.)
        (into-array
          (vector
            (double-array )
            (double-array )
            (double-array )))))