Clojure constructor varargs gen-class

in the map of constructors and the subsequent -bounded definitions, how can I represent the varargs constructor (assuming the superclass has several constructors, of which one is varargs)?

+7
source share
2 answers

Since varargs are essentially syntactic sugar for Object arrays, you can simply use "[Ljava.lang.Object;" as the type of constructor parameter.

Here is a sample code:

(ns t.vtest (:gen-class :implements [clojure.lang.IDeref] :init init :state state :constructors {["[Ljava.lang.Object;"] []})) ;; ^----------------------- ;; You should put "[Ljava.lang.Object;" for superclass varargs constructor here ;; I left it blank for the sake of working example (defn -init [args] (println "first element of args" (aget args 0) "total elements" (alength args)) [[] (into [] args)]) (defn -deref [this] (.state this)) 

and what does it look like in repl

 user=> @(t.vtest. (into-array Object ["A" "B" 1 2])) first element of args A total elements 4 ["A" "B" 1 2] 
+1
source

Since clojure does not support it at the moment you need to fix it: https://groups.google.com/forum/#!topic/clojure/HMpMavh0WxA.

And use it with the new meta tag:

 (ns t.vtest (:gen-class :implements [clojure.lang.IDeref] :init init :state state :constructors {^:varargs ["[Ljava.lang.Object;"] []} )) 
+1
source

All Articles