Uploaded image for project: 'Clojure'
  1. CLJ-440

java method calls cannot omit varargs


    • Type: New Feature
    • Status: Open
    • Priority: Critical
    • Resolution: Unresolved
    • Affects versions: None
    • Fix versions: None
    • Labels:
    • Approval:



      Clojure calls to Java vararg methods require creating an object array for the final arg. This is a frequent source of confusion when doing interop.

      E.g., trying to call java.util.Collections.addAll(Collection c, T... elements):

      user=> (Collections/addAll [] (object-array 0))
      user=> (Collections/addAll [])
      IllegalArgumentException No matching method: addAll  clojure.lang.Compiler$StaticMethodExpr.<init> (Compiler.java:1401)

      The Method class provides an isVarArg() method, which could be used to inform the compiler to process things differently.

      From http://groups.google.com/group/clojure/browse_thread/thread/7d0d6cb32656a621

      Latest patch: Removed because incomplete and goal not clear

      Varargs in Java

      As currently stated, the scope of this ticket is only to omit varargs, but this is only one case where Clojures handling of varargs differs from Java. For completeness, here is a brief survey of how Java handles vararg methods, which could hopefully inform a discussion for how Clojure could do things differently, and what the goal of this ticket should be.

      Given the following setup:

      public class VarArgs {
          public static class SingleVarargMethod {
              public static void m(String arg1, String... args) {}
          public static class MultipleVarargMethods {
              public static void m(String... args) {}
              public static void m(String arg1) {}
              public static void m(String arg1, String... args) {}
      Java Possible clojure equivalent? Comments
      VarArgs.SingleVarargMethod.m("a"); (SingleVarargMethod/m "a")  
      VarArgs.SingleVarargMethod.m("a", "b"); (SingleVarargMethod/m "a" "b")  
      VarArgs.SingleVarargMethod.m("a", "b", "c"); (SingleVarargMethod/m "a" "b" "c")  
      VarArgs.SingleVarargMethod.m("a", new String[]{"b", "c"}); (SingleVarargMethod/m "a" (object-array ["b" "c"]))  
      VarArgs.MultipleVarargMethods.m(); (MultipleVarargMethods/m)  
      VarArgs.MultipleVarargMethods.m((String) null); (MultipleVarargMethods/m nil) Use type hints to disambiguate?
      VarArgs.MultipleVarargMethods.m((String[]) null); (MultipleVarargMethods/m nil) Use type hints to disambiguate?
      VarArgs.MultipleVarargMethods.m("a", null); (MultipleVarargMethods/m "a" nil)  
      VarArgs.MultipleVarargMethods.m("a", new String[]{}); (MultipleVarargMethods/m "a" (object-array 0))  
      VarArgs.MultipleVarargMethods.m(new String[]{"a"}); (MultipleVarargMethods/m (object-array ["a"]))  
      VarArgs.MultipleVarargMethods.m("a", new String[]{"b", "c"}); (MultipleVarargMethods/m "a" (object-array ["b" "c"]))  




            • Assignee:
              ragge Ragnar Dahlén
              ataggart Alexander Taggart
            • Votes:
              7 Vote for this issue
              6 Start watching this issue


              • Created: