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?
into-arrayhas a 2-arity version, where the first argument is a component type. Note the second argument is a sequence, so there's no need for using a loop inbuild-fruits. I would provide a code sample, but as Michiel points out, the repro code in the question is not standalone, so it's hard to provide an example.LocalDate, wheremapis used to transform the input maps toLocalDateinstances.