如何在clojure中处理java变量长度参数? [英] How to handle java variable length arguments in clojure?

查看:100
本文介绍了如何在clojure中处理java变量长度参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一个java lib包装到clojure,但我有处理可变长度参数的问题。说,

  TestClass.aStaticFunction(Integer ... intList){/*....*/} 



如何在clojure中调用此函数?解决方案

由于Java varargs是实际上是数组,你可以通过传递数组在Clojure中调用vararg函数。



您可以将Clojure seq(也许使用Clojure的各种变量参数函数)转换为数组:

 (TestClass / aStaticFunction(into-array Integer [(int 1),(int 2)]))

 (defn a-static-function-wrapper [& args] 
(TestClass / aStaticFunction(into-array Integer args))

手动设置其索引

 (TestClass / aStaticFunction(doto(make-array Integer 3)
(aset 0 first-元素)
(aset 1 second-element)
(aset 2 third-element)))


I'am wrapping a java lib into clojure, but i have problems dealing with variable length arguments. Say,

TestClass.aStaticFunction(Integer... intList){/*....*/}

How could i call this function in clojure?

解决方案

Since Java varargs are actually arrays, you can call vararg functions in Clojure by passing an array.

You could convert a Clojure seq (maybe by using Clojure's variety of variable argument functions) into an array:

 (TestClass/aStaticFunction (into-array Integer [(int 1),(int 2)]))

or

(defn a-static-function-wrapper [& args]
  (TestClass/aStaticFunction (into-array Integer args))

Or make an array and set its indices manually

(TestClass/aStaticFunction (doto (make-array Integer 3)
                              (aset 0 first-element)
                              (aset 1 second-element)
                              (aset 2 third-element)))

这篇关于如何在clojure中处理java变量长度参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆