On Oct 3, 2:00 pm, Stuart Halloway <[EMAIL PROTECTED]> wrote:
> Does this make sense?
>
> user=> (let [x Integer] (.getName x))
> "java.lang.Integer"
> user=> (.getName Integer)
> java.lang.NoSuchFieldException: getName
This is a FAQ, but as there is no Clojure FAQ I'll try to give a
canonic answer here:
(.member x) is shorthand for (. x member)
. is a special operator. In particular, it does non-standard
evaluation of its first arg.
Because . can call static and instance methods, it determines if it is
a static call be seeing if the first arg names a class. In this case
it does:
(. Integer getName)
so it tries to find the static getName member and doesn't, because
there isn't one. You could use this syntax to call a static method on
Integer:
(. Integer parseInt "42")
Everywhere else, a classname designates the class object.
In short, if you can't say x.y() in Java you can't say (. x y) in
Clojure, and you can't say Integer.getName() in Java.
The workaround is, you really want the class object here, and to
suppress the use of Integer as a class scope:
(. (identity Integer) getName)
The above calls an instance method on an object of type Class.
The use of (. classname staticMember) should be deprecated, as we now
have the superior Classname/staticMember:
(Integer/parseInt "42")
but I think that it is a tough thing to switch to (. x member) is for
instance members only, maybe after a period of deprecation with
warnings...
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---