clojure gen-class varargs 构造函数

发布于 2024-12-28 05:28:24 字数 84 浏览 2 评论 0原文

在 :constructors 映射和后续 -init 定义中,如何表示 varargs 构造函数(假设超类有多个构造函数,其中一个是 varargs)?

in the :constructors map and subsequent -init definitions, how do I represent a varargs constructor (assuming the superclass has multiple constructors of which one is varargs) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2025-01-04 05:28:24

由于可变参数本质上是对象数组的语法糖,因此您可以只使用“[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]

Since varargs are essentially syntax sugar for Object arrays, you could just use "[Ljava.lang.Object;" as the type of constructor's parameter.

Here's some 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 that's how it looks in REPL

user=> @(t.vtest. (into-array Object ["A" "B" 1 2]))
first element of args A total elements 4
["A" "B" 1 2]
む无字情书 2025-01-04 05:28:24

由于 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;"] []} 
  ))

Since clojure don't support it at the moment you need to patch it with: https://groups.google.com/forum/#!topic/clojure/HMpMavh0WxA.

And use it with new meta tag:

(ns t.vtest 
  (:gen-class 
   :implements   [clojure.lang.IDeref]
   :init init
   :state state
   :constructors {^:varargs ["[Ljava.lang.Object;"] []} 
  ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文