Problems calling a variadic Java function from Clojure

kittylyst :

I'm having a play with the Java NIO.2 API from JDK 7.

In particular, I want to call the method: Paths#get(String first, String... more)

This is a static method which takes in at least one string, and returns a Path object corresponding to it. There's an overloaded form: Paths#get(URI uri)

However, I can't seem to call the top method from Clojure. The nearest I can seem to get is this:

(Paths/get ^String dir-fq (object-array 0))

which fails with:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

as you might expect. After all, we're passing in an Object[] to something that's expecting String[].

I've tried removing the (object-array) form - but that just causes Clojure to try to call the get(URI) method - both with and without the type hint.

Passing nil as the second argument to Paths#get(String, String...) causes the right method to be called, but Java 7 then fails with an NPE.

I can't seem to find a way in Clojure to express the type String[] - I'm guessing I either need to do that or provide a hint to the dispatch system.

Any ideas?

amalloy :

As you noticed, it doesn't want an Object[], it wants a String[]. object-array does exactly what it says: it makes an array of objects. If you want to create an array with some different type, make-array and into-array are your friends. For example here:

(Paths/get "foo" (into-array String ["bar" "baz"]))

The String specifier there is optional in this case: if you leave out the array's desired type, Clojure uses the type of the first object as the array's component type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling clojure from java

From Java

Java variadic function parameters

From Java

Calling java functions from Clojure

From Java

Calling Java from Clojure

From Java

Calling a very simple clojure function from Java does not work

From Java

Problems Calling a Java Class from JRuby

From Java

How to invoke Clojure function directly from Java

From Java

calling clojure from Java (Clojure Interop)

From Dev

Calling variadic template function with no args failing

From Dev

Calling a Clojure Function from Haskell

From Dev

Problems calling a postgresql function from python

From Dev

Variadic Function in Java Script

From Dev

Calling a Java static function from Clojure

From Dev

Get calling function name in Clojure

From Dev

Calling a variadic function in a Jenkinsfile fails unpredictably

From Dev

NoClassDefFoundError calling Clojure from Java (Android, LibGDX)

From Dev

Python - Function calling problems

From Dev

Calling a method on a Java class from Clojure

From Dev

Problems calling PHP function from Javascript

From Dev

Clojure: Variadic function for vector addition

From Dev

Python: Problems in calling the function from the while loop

From Dev

Calling a variadic template function with a member function

From Dev

Recursive variadic clojure function (unboxing/destructuring a list when calling a function)

From Dev

Calling a java function with an optionally null argument from clojure

From Dev

Calling a function within conj in clojure

From Dev

Calling a Java variadic function from C through the JNI

From Dev

Problems calling a clojure function that takes a map as parameter from java

From Dev

Calling java function from dart

From Dev

Calling base function from variadic template class in c++14

Related Related

  1. 1

    Calling clojure from java

  2. 2

    Java variadic function parameters

  3. 3

    Calling java functions from Clojure

  4. 4

    Calling Java from Clojure

  5. 5

    Calling a very simple clojure function from Java does not work

  6. 6

    Problems Calling a Java Class from JRuby

  7. 7

    How to invoke Clojure function directly from Java

  8. 8

    calling clojure from Java (Clojure Interop)

  9. 9

    Calling variadic template function with no args failing

  10. 10

    Calling a Clojure Function from Haskell

  11. 11

    Problems calling a postgresql function from python

  12. 12

    Variadic Function in Java Script

  13. 13

    Calling a Java static function from Clojure

  14. 14

    Get calling function name in Clojure

  15. 15

    Calling a variadic function in a Jenkinsfile fails unpredictably

  16. 16

    NoClassDefFoundError calling Clojure from Java (Android, LibGDX)

  17. 17

    Python - Function calling problems

  18. 18

    Calling a method on a Java class from Clojure

  19. 19

    Problems calling PHP function from Javascript

  20. 20

    Clojure: Variadic function for vector addition

  21. 21

    Python: Problems in calling the function from the while loop

  22. 22

    Calling a variadic template function with a member function

  23. 23

    Recursive variadic clojure function (unboxing/destructuring a list when calling a function)

  24. 24

    Calling a java function with an optionally null argument from clojure

  25. 25

    Calling a function within conj in clojure

  26. 26

    Calling a Java variadic function from C through the JNI

  27. 27

    Problems calling a clojure function that takes a map as parameter from java

  28. 28

    Calling java function from dart

  29. 29

    Calling base function from variadic template class in c++14

HotTag

Archive