generating a static method

590 views
Skip to first unread message

.Bill Smith

unread,
Mar 9, 2009, 10:52:31 PM3/9/09
to Clojure
Would someone mind showing me a brief example of how to define a
static method (using gen-class)?

Thanks,
Bill Smith
Austin, TX

.Bill Smith

unread,
Mar 10, 2009, 12:03:15 AM3/10/09
to Clojure
The genclass documentation says, "Static methods can be specified with
#^{:static true} in the signature's metadata." I thought that would
mean this:

(ns tango.test.unit.StaticTest
(:gen-class
:methods [[f [] #^{:static true} void ]]))

(defn -init [_] ())

(defn -f [] (println "hello world!"))

But that doesn't seem to do the trick:

~/projects/junit-clojure$ javap -classpath /home/bsmith/software/
clojure/trunk/clojure.jar:/home/bsmith/software/junit-4.4/
junit-4.4.jar:$PWD:$PWD/classes tango.test.unit.StaticTest
public class tango.test.unit.StaticTest extends java.lang.Object{
public static {};
public tango.test.unit.StaticTest();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public java.lang.Object clone();
public int hashCode();
public void f();
public static void main(java.lang.String[]);
}

I'm using Clojure rev 1327.

Bill Smith
Austin, Texas

Adrian Cuthbertson

unread,
Mar 10, 2009, 12:17:20 AM3/10/09
to clo...@googlegroups.com
HI Bill,

I also tried the metadata tag and couldn't get it to work, but the
following does...

(ns gncls.MyStatic
(:gen-class
:methods [[say-hi [String] String]]))

(defn -say-hi
[this who]
(str "Hi " who))

(defn -say-static-hi
[who]
(str "Hi " who))


user=> (compile 'gncls.MyStatic)
user=> (.say-hi (gncls.MyStatic.) "Bill")
"Hi Bill"
user=> (gncls.MyStatic/-say-hi "" "Bill")
"Hi Bill"

user=> (gncls.MyStatic/-say-static-hi "Bill")
"Hi Bill"

That is, a static method is automatically created for the "prefixed"
method which you can call directly. You could make use of :prefix in
:gen-class to tidy this up.

I'm not sure if this is the whole story though.

Regards, Adrian.

Christophe Grand

unread,
Mar 10, 2009, 3:18:13 AM3/10/09
to clo...@googlegroups.com
.Bill Smith a écrit :

> The genclass documentation says, "Static methods can be specified with
> #^{:static true} in the signature's metadata." I thought that would
> mean this:
>
> (ns tango.test.unit.StaticTest
> (:gen-class
> :methods [[f [] #^{:static true} void ]]))
>
>

Try:

(ns tango.test.unit.StaticTest
(:gen-class
:methods [#^{:static true} [f [] void ]]))


(untested but that's where gen-class expects the metadata to be/)

HTH

Christophe


--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)


Adrian Cuthbertson

unread,
Mar 10, 2009, 3:51:21 AM3/10/09
to clo...@googlegroups.com
Hi Christophe,

It works as per your example, but not with arguments to the method...

ns gncls.MyStatic
(:gen-class
:methods [#^{:static true} [f [String] void ]]))

(defn -f
[s] ; also [this s] doesn't work
(prn "Hi from " s ))

(gncls.MyStatic/f "me")
java.lang.Exception: No such var: gncls.MyStatic/f (NO_SOURCE_FILE:2)

Christophe Grand

unread,
Mar 10, 2009, 4:21:16 AM3/10/09
to clo...@googlegroups.com
Adrian Cuthbertson a écrit :

> Hi Christophe,
>
> It works as per your example, but not with arguments to the method...
>
> ns gncls.MyStatic
> (:gen-class
> :methods [#^{:static true} [f [String] void ]]))
>
> (defn -f
> [s] ; also [this s] doesn't work
> (prn "Hi from " s ))
>
> (gncls.MyStatic/f "me")
> java.lang.Exception: No such var: gncls.MyStatic/f (NO_SOURCE_FILE:2

Hmm, the exception says it is looking for gncls.MyStatic/f, not for
gncls.MyStatic/-f, it hints me that you are not going through the static
method at all but directly to the function through the namespace
gncls.MyStatix. Try and contrast:

(ns gncls.MyStatic


(:gen-class
:methods [#^{:static true} [f [String] void ]]))

(defn -f
[s]
(prn "Hi " s "from -f"))

(defn f
[s]
(prn "Hi " s "from f"))

(gncls.MyStatic/f "me") ; should print "Hi me from f"
(. gncls.MyStatic f "me") ; should print "Hi me from -f"

a/b both means function b in ns a and static method b in class a, when you have both a ns and a class going by the same name the ns wins.

.Bill Smith

unread,
Mar 10, 2009, 9:09:47 AM3/10/09
to Clojure
That worked. Thank you for your help, Christophe.

Bill Smith,
Austin, TX

Mark Volkmann

unread,
Mar 10, 2009, 9:43:25 AM3/10/09
to clo...@googlegroups.com
On Mon, Mar 9, 2009 at 9:52 PM, .Bill Smith <william...@gmail.com> wrote:
>
> Would someone mind showing me a brief example of how to define a
> static method (using gen-class)?

Look for "static" under
http://www.ociweb.com/mark/clojure/article.html#Compiling.

--
R. Mark Volkmann
Object Computing, Inc.

Reply all
Reply to author
Forward
0 new messages