8

:constructors マップとそれに続く -init 定義では、どうすれば varargs コンストラクターを表現できますか?

4

2 に答える 2

2

varargs は基本的に Object 配列のシンタックス シュガーであるため、"[Ljava.lang.Object;" を使用できます。コンストラクターのパラメーターの型として。

サンプルコードは次のとおりです。

(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))

それがREPLでどのように見えるかです

user=> @(t.vtest. (into-array Object ["A" "B" 1 2]))
first element of args A total elements 4
["A" "B" 1 2]
于 2012-02-07T14:37:41.850 に答える
1

現時点では clojure はサポートしていないため、パッチを適用する必要があります: https://groups.google.com/forum/#!topic/clojure/HMpMavh0WxA.

そして、新しいメタタグでそれを使用します:

(ns t.vtest 
  (:gen-class 
   :implements   [clojure.lang.IDeref]
   :init init
   :state state
   :constructors {^:varargs ["[Ljava.lang.Object;"] []} 
  ))
于 2013-09-11T09:48:46.927 に答える