Class Literals Bug

8 views
Skip to first unread message

Henk Boom

unread,
Feb 6, 2008, 5:08:26 PM2/6/08
to clo...@googlegroups.com
Unless I misunderstand how class literals work, there's something
wrong with the second command not working:

user=> ((fn [c] (. c (getFields))) Math)
[Ljava.lang.reflect.Field;@770d2e
user=> (. Math (getFields))
clojure.lang.Compiler$CompilerException: REPL:2: No matching method: getFields
at clojure.lang.Compiler.analyzeSeq(Compiler.java:3183)
at clojure.lang.Compiler.analyze(Compiler.java:3125)
at clojure.lang.Compiler.analyze(Compiler.java:3100)
at clojure.lang.Compiler.eval(Compiler.java:3203)
at clojure.lang.Repl.main(Repl.java:63)
Caused by: java.lang.IllegalArgumentException: No matching method: getFields
at clojure.lang.Compiler$StaticMethodExpr.<init>(Compiler.java:1039)
at clojure.lang.Compiler$HostExpr$Parser.parse(Compiler.java:683)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:3176)
... 4 more

Henk

Rich Hickey

unread,
Feb 6, 2008, 5:26:21 PM2/6/08
to Clojure


On Feb 6, 5:08 pm, "Henk Boom" <lunarcri...@gmail.com> wrote:
> Unless I misunderstand how class literals work, there's something
> wrong with the second command not working:
>
> user=> ((fn [c] (. c (getFields))) Math)
> [Ljava.lang.reflect.Field;@770d2e
> user=> (. Math (getFields))
> clojure.lang.Compiler$CompilerException: REPL:2: No matching method: getFields

. is a special operator, not a function. As such, it does special
processing of its arguments, in this case it needs to distinguish
instance from static members, and it does so by examining the _form_
(not value!) of its first argument. If the form names or is a class,
it's treated as a static call. By saying (. Math ...) you are
indicating a static member of the Math class, when what you want is to
call an instance method on the Math class object. You can do that like
this:

(. (identity Math) (getFields))

-> [Ljava.lang.reflect.Field;@cb18b5

Rich

Henk Boom

unread,
Feb 7, 2008, 12:52:52 PM2/7/08
to clo...@googlegroups.com
On 06/02/2008, Rich Hickey <richh...@gmail.com> wrote:
> . is a special operator, not a function. As such, it does special
> processing of its arguments, in this case it needs to distinguish
> instance from static members, and it does so by examining the _form_
> (not value!) of its first argument. If the form names or is a class,
> it's treated as a static call. By saying (. Math ...) you are
> indicating a static member of the Math class, when what you want is to
> call an instance method on the Math class object. You can do that like
> this:
>
> (. (identity Math) (getFields))
>
> -> [Ljava.lang.reflect.Field;@cb18b5

Ahh, tricky. I guess this is similar to how Math.getFields() does not
work, you must do Math.getClass().getFields(). The difference is that
in clojure there is not distinct notation for Math and
Math.getClass(), except that one is handled by a macro and the other
is evaluated.

Thanks for the help,
Henk

Rich Hickey

unread,
Feb 7, 2008, 3:45:47 PM2/7/08
to Clojure


On Feb 7, 12:52 pm, "Henk Boom" <lunarcri...@gmail.com> wrote:
> On 06/02/2008, Rich Hickey <richhic...@gmail.com> wrote:
>
> > . is a special operator, not a function. As such, it does special
> > processing of its arguments, in this case it needs to distinguish
> > instance from static members, and it does so by examining the _form_
> > (not value!) of its first argument. If the form names or is a class,
> > it's treated as a static call. By saying (. Math ...) you are
> > indicating a static member of the Math class, when what you want is to
> > call an instance method on the Math class object. You can do that like
> > this:
>
> > (. (identity Math) (getFields))
>
> > -> [Ljava.lang.reflect.Field;@cb18b5
>
> Ahh, tricky. I guess this is similar to how Math.getFields() does not
> work, you must do Math.getClass().getFields(). The difference is that
> in clojure there is not distinct notation for Math and
> Math.getClass()

Yes there is:

(. Math ... ===> Math (scope)

Math anywhere else ===> Math.class (object)

Rich
Reply all
Reply to author
Forward
0 new messages