trouble invoking static method

37 views
Skip to first unread message

ctu

unread,
Nov 20, 2007, 1:30:40 PM11/20/07
to Clojure
user=> (. java.lang.Math (abs 3))

java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:
96)
at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:
143)
at clojure.lang.Compiler$StaticMethodExpr.eval(Compiler.java:
907)
at clojure.lang.Compiler.eval(Compiler.java:2636)
at clojure.lang.Repl.main(Repl.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)

clojure version 20071029

java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode)

bad usage ?

.Bill Smith

unread,
Nov 20, 2007, 4:24:30 PM11/20/07
to Clojure
user=> (.. 3 (getClass) (getName))
"clojure.lang.FixNum"

Clojure doesn't automatically convert 3 to a Java primitive. Instead
you can do this:

user=> (. java.lang.Math (abs (long 3)))
3
user=> (. java.lang.Math (abs (int 3)))
3

-Bill

ctu

unread,
Nov 21, 2007, 6:09:42 AM11/21/07
to Clojure

ok thx.

On 20 nov, 22:24, ".Bill Smith" <william.m.sm...@gmail.com> wrote:
> user=> (.. 3 (getClass) (getName))
> "clojure.lang.FixNum"

good check to remember ;)


"wrong number of arguments" exception message was a little misleading.

.Bill Smith

unread,
Nov 21, 2007, 9:07:10 AM11/21/07
to Clojure
> "wrong number of arguments" exception message was a little misleading.

Agreed. Clojure will eventually need to do a better job of error
reporting.

-Bill

Rich Hickey

unread,
Nov 21, 2007, 11:52:31 AM11/21/07
to Clojure
A better error message and enhanced numeric conversion logic are both
in Reflector.java, in SVN now.

With the enhancement the coercion function is no longer required for
your example. The matching is still pretty strict, so there will still
be cases where coercion is required to force a match.

When no match can be found the error will now be "No matching field or
method found".

Thanks for the report,

Rich
Reply all
Reply to author
Forward
0 new messages