Reputation: 408
This is my code. I would like to make an array of Fruit (an custom java object class).
(defn build-fruit
[part]
(let [name (:name part)
color (:color part)
price (:price part)
is_available (:is_available part)]
(-> (Fruit/newBuilder)
(.setName name)
(.setColor color)
(.setPrice price)
(.setIsAvailable is_available)
(.build))))
(defn build-fruits
[result length]
(loop [remaining-fruits result
final-fruits (make-array Fruit length)]
(if (empty? remaining-fruits)
final-fruits
(let [[part & remaining] remaining-fruits]
(recur remaining
(into-array final-fruits (build-fruit part)))))) ;got an error here
)
(defn -getAllFruits [this ^Empty req ^StreamObserver res]
(let [result (clj-grpc.database/findAllFruit)
length (count result)
fruits (build-fruits result length)]
(doto res
(.onNext (-> (ListOfFruits/newBuilder)
(.addAllFruit fruits)
(.build)))
(.onCompleted))))
But got an error
java.lang.ClassCastException: [Lorg.example.fruitproto.Fruit; cannot be cast to java.lang.Class
at clojure.core$into_array.invokeStatic(core.clj:3431)
at clojure.core$into_array.invoke(core.clj:3431)
at clj_grpc.fruitservice$build_fruits.invokeStatic(fruitservice.clj:118)
at clj_grpc.fruitservice$build_fruits.invoke(fruitservice.clj:110)
My guess is I cann't use make-array
. I also can't use simple array like this []
because it also got another error. What is the proper way to achieve this?
Upvotes: 1
Views: 245
Reputation: 2201
Here is a standalone example of using into-array
, building an array of LocalDate
rather than Fruit
:
(ns overflow.example
(:import (java.time LocalDate)))
(defn build-local-date [{:keys [year month day]}]
(LocalDate/of year month day))
(defn build-local-dates [xs]
(into-array LocalDate (map build-local-date xs)))
Upvotes: 2