Clojure China

如果想自定义静态函数 其参数为数组 那么原类型只能用Object么?

clojure
#1

原文:

ps:
查了一下,只对java.lang中的类型有效,那么Array或者数组岂不是无效了?
我用:methods [#^{:static true} [show [Object] Object]]虽然通过了compile,效果也达到了,似乎这个Object太万金油了,能不能更精确一些类型呢?
谢谢

#2

clojure 里面是支持基本类型数组的 type hints 的,例如:^longs,但是在gen-class 里面用,会报错。我这里用 Long 绕过去了

(defmacro gen-my-class []
  (let [clazz (Class/forName "[Ljava.lang.Long;")]
    `(do (gen-class
          :name com.example.Demo
          :methods [[~'foo [~clazz] ~clazz]]))))

(defn -foo [this xs]
  (into-array  (map inc (seq xs))))

Java 代码:

public class GenDemo {
    public static void main(String[] args) {
        Long[] xs = new Long[] {1L, 2L, 3L};
        System.out.println(Arrays.toString(new Demo().foo(xs)));
    }
}

输出:[2, 3, 4]

1赞