Great patch to allow access to this functionality. However, to my eye it
looks kinda messy to do something so 'normal'. How difficult would it be to
implement some syntactic sugar to "do the right thing" (add the appropriate
metadata and massage the type of the final arg):
(ns foo
(:gen-class
:name Foo
:methods [ [foo [& String] void]]
))
Using '&' would be consistent with Clojure's syntax but I would imagine it
would need to be 'allowed' on the function definition as well to keep
things tidy:
(defn -foo [& string-array] (...))
Maybe this would work anyway without the '&'?
On Thursday, 3 May 2012 10:16:25 UTC+1, sw1nn wrote:
>
> Hi,
>
> On SO recently, someone asked how to generate a varargs method with
> gen-class [1]
>
> i.e how can the equivalent of this method be generated:
>
> void foo(String... args) { ...}
>
> I did some investigation into a possible answer.
>
> Thinking that varargs methods in java are a trick of the compiler I
> expected to be able to generate
>
> void foo(String[] args) {...}
>
> with this...
>
> (ns foo
> (:gen-class
> :name Foo
> :methods [[foo ["[Ljava.lang.String;"] void]]
> ))
>
> and for it to 'just work'(tm). decompiling the generated class I get...
>
> $ javap -classpath . Foo
> public class Foo extends java.lang.Object{
> public static {};
> public Foo();
> public java.lang.Object clone();
> public int hashCode();
> public java.lang.String toString();
> public boolean equals(java.lang.Object);
> public void foo(java.lang.String[]);
> public static void main(java.lang.String[]);
> }
>
> The problem is that the foo method is not recognized as a varargs method
> if you compile against it.
>
> After some digging I discovered that for a method to be recognized as a
> varargs, the ACC_VARARGS flag must be on the method in the bytecode.
>
> In the clojure-1.4.0 src I found that the flag is defined in
> ./src/jvm/clojure/asm/Opcodes.java but is never referenced anywhere, which
> leads me to the conclusion that currently it isn't possible to generate a
> varargs method in clojure.
>
> Am I missing something?
>
> Should I raise this as an issue on JIRA?
>
>
> [1] http://stackoverflow.com/q/10414075/808653
>
> Neale
> {t: @sw1nn <https://twitter.com/#!/sw1nn>, w: sw1nn.com }
>
>
On Thursday, 3 May 2012 10:16:25 UTC+1, sw1nn wrote:
>
> Hi,
>
> On SO recently, someone asked how to generate a varargs method with
> gen-class [1]
>
> i.e how can the equivalent of this method be generated:
>
> void foo(String... args) { ...}
>
> I did some investigation into a possible answer.
>
> Thinking that varargs methods in java are a trick of the compiler I
> expected to be able to generate
>
> void foo(String[] args) {...}
>
> with this...
>
> (ns foo
> (:gen-class
> :name Foo
> :methods [[foo ["[Ljava.lang.String;"] void]]
> ))
>
> and for it to 'just work'(tm). decompiling the generated class I get...
>
> $ javap -classpath . Foo
> public class Foo extends java.lang.Object{
> public static {};
> public Foo();
> public java.lang.Object clone();
> public int hashCode();
> public java.lang.String toString();
> public boolean equals(java.lang.Object);
> public void foo(java.lang.String[]);
> public static void main(java.lang.String[]);
> }
>
> The problem is that the foo method is not recognized as a varargs method
> if you compile against it.
>
> After some digging I discovered that for a method to be recognized as a
> varargs, the ACC_VARARGS flag must be on the method in the bytecode.
>
> In the clojure-1.4.0 src I found that the flag is defined in
> ./src/jvm/clojure/asm/Opcodes.java but is never referenced anywhere, which
> leads me to the conclusion that currently it isn't possible to generate a
> varargs method in clojure.
>
> Am I missing something?
>
> Should I raise this as an issue on JIRA?
>
>
> [1] http://stackoverflow.com/q/10414075/808653
>
> Neale
> {t: @sw1nn <https://twitter.com/#!/sw1nn>, w: sw1nn.com }
>
>
On Thursday, 3 May 2012 10:16:25 UTC+1, sw1nn wrote:
>
> Hi,
>
> On SO recently, someone asked how to generate a varargs method with
> gen-class [1]
>
> i.e how can the equivalent of this method be generated:
>
> void foo(String... args) { ...}
>
> I did some investigation into a possible answer.
>
> Thinking that varargs methods in java are a trick of the compiler I
> expected to be able to generate
>
> void foo(String[] args) {...}
>
> with this...
>
> (ns foo
> (:gen-class
> :name Foo
> :methods [[foo ["[Ljava.lang.String;"] void]]
> ))
>
> and for it to 'just work'(tm). decompiling the generated class I get...
>
> $ javap -classpath . Foo
> public class Foo extends java.lang.Object{
> public static {};
> public Foo();
> public java.lang.Object clone();
> public int hashCode();
> public java.lang.String toString();
> public boolean equals(java.lang.Object);
> public void foo(java.lang.String[]);
> public static void main(java.lang.String[]);
> }
>
> The problem is that the foo method is not recognized as a varargs method
> if you compile against it.
>
> After some digging I discovered that for a method to be recognized as a
> varargs, the ACC_VARARGS flag must be on the method in the bytecode.
>
> In the clojure-1.4.0 src I found that the flag is defined in
> ./src/jvm/clojure/asm/Opcodes.java but is never referenced anywhere, which
> leads me to the conclusion that currently it isn't possible to generate a
> varargs method in clojure.
>
> Am I missing something?
>
> Should I raise this as an issue on JIRA?
>
>
> [1] http://stackoverflow.com/q/10414075/808653
>
> Neale
> {t: @sw1nn <https://twitter.com/#!/sw1nn>, w: sw1nn.com }
>
>
On Thursday, 3 May 2012 10:16:25 UTC+1, sw1nn wrote:
>
> Hi,
>
> On SO recently, someone asked how to generate a varargs method with
> gen-class [1]
>
> i.e how can the equivalent of this method be generated:
>
> void foo(String... args) { ...}
>
> I did some investigation into a possible answer.
>
> Thinking that varargs methods in java are a trick of the compiler I
> expected to be able to generate
>
> void foo(String[] args) {...}
>
> with this...
>
> (ns foo
> (:gen-class
> :name Foo
> :methods [[foo ["[Ljava.lang.String;"] void]]
> ))
>
> and for it to 'just work'(tm). decompiling the generated class I get...
>
> $ javap -classpath . Foo
> public class Foo extends java.lang.Object{
> public static {};
> public Foo();
> public java.lang.Object clone();
> public int hashCode();
> public java.lang.String toString();
> public boolean equals(java.lang.Object);
> public void foo(java.lang.String[]);
> public static void main(java.lang.String[]);
> }
>
> The problem is that the foo method is not recognized as a varargs method
> if you compile against it.
>
> After some digging I discovered that for a method to be recognized as a
> varargs, the ACC_VARARGS flag must be on the method in the bytecode.
>
> In the clojure-1.4.0 src I found that the flag is defined in
> ./src/jvm/clojure/asm/Opcodes.java but is never referenced anywhere, which
> leads me to the conclusion that currently it isn't possible to generate a
> varargs method in clojure.
>
> Am I missing something?
>
> Should I raise this as an issue on JIRA?
>
>
> [1] http://stackoverflow.com/q/10414075/808653
>
> Neale
> {t: @sw1nn <https://twitter.com/#!/sw1nn>, w: sw1nn.com }
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en