java interop, `(.instanceMember Classname)`

124 views
Skip to first unread message

John Gabriele

unread,
Mar 20, 2017, 3:25:09 AM3/20/17
to Clojure
In the [Java Interop Docs](https://clojure.org/reference/java_interop), what does the example

    (.instanceMember Classname args*)

mean? (Looks like the example given at the top for that one is `(.getName String)`, but I don't see any `getName` method in the javadoc for java.lang.String.)

It's been a while since I've used Java. I understand the other examples there (accessing static and instance fields/members, and calling static and instance methods), but I don't understand what that above one means.

Thanks!

Matching Socks

unread,
Mar 20, 2017, 4:14:46 AM3/20/17
to Clojure
Methods having the same name might be distinguished by their argument lists.

Matching Socks

unread,
Mar 20, 2017, 4:26:19 AM3/20/17
to Clojure
In (.getName String) String is an instance of the class Class.

John Gabriele

unread,
Mar 20, 2017, 4:11:43 PM3/20/17
to Clojure
On Monday, March 20, 2017 at 4:14:46 AM UTC-4, Matching Socks wrote:
Methods having the same name might be distinguished by their argument lists.

Thanks, but it sounds like you're describing method overloading, as in

    (.someMethod someObj arg1) ; vs
    (.someMethod someObj arg1 arg2) ; different arity gets different method

whereas I think I'm asking about what the `(.someMethod SomeClass ...)` syntax is supposed to mean (where it looks like one is trying to call an instance method on a Class (?).

Am I misunderstanding your reply?

Thanks!

Gregg Reynolds

unread,
Mar 20, 2017, 4:32:27 PM3/20/17
to clo...@googlegroups.com
it's just an example of the difficulty of documenting abstractions.  just replace "SomeClass" with "instanceOfSomeClass"

Gregg Reynolds

unread,
Mar 20, 2017, 4:38:31 PM3/20/17
to clo...@googlegroups.com
please ignore! sleep deprived.

Gregg Reynolds

unread,
Mar 20, 2017, 4:50:03 PM3/20/17
to clo...@googlegroups.com
On Mar 20, 2017 3:11 PM, "John Gabriele" <jmg...@gmail.com> wrote:

String inherits from Object, which has a getName method.

Am I misunderstanding your reply?

Thanks!

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Fleming

unread,
Mar 20, 2017, 4:59:33 PM3/20/17
to clo...@googlegroups.com
Object doesn't have a getName() method.

This doc is confusing - as Phill comments above, this is calling the getName() method on an instance of Class. In Clojure, a bare classname (String, ArrayList or whatever) resolves to the class itself if it has been imported (i.e. what would be String.class in Java). The doc is confusing because (.instanceMember Classname args*) is really just a special case of (.instanceMember instance args*), where "instance" refers to an instance of a Class object. I'm not sure why that doc makes that distinction, it doesn't seem useful to me.

Gregg Reynolds

unread,
Mar 20, 2017, 5:13:37 PM3/20/17
to clo...@googlegroups.com


On Mar 20, 2017 3:59 PM, "Colin Fleming" <colin.ma...@gmail.com> wrote:
Object doesn't have a getName() method.

sorry, of course you're right. it's java.lang.Class that has getName. as you point out it's the doc that is suboptimal.

Alex Miller

unread,
Mar 20, 2017, 7:47:46 PM3/20/17
to Clojure
If someone could file an issue on the clojure-site repo, I would be happy to improve the example.

John Gabriele

unread,
Mar 21, 2017, 12:22:33 PM3/21/17
to Clojure


On Monday, March 20, 2017 at 7:47:46 PM UTC-4, Alex Miller wrote:
If someone could file an issue on the clojure-site repo, I would be happy to improve the example.

Thanks. Filed: <https://github.com/clojure/clojure-site/issues/171>, though I don't have alternative wording/prose for it.

John Gabriele

unread,
Mar 21, 2017, 12:35:05 PM3/21/17
to Clojure
On Monday, March 20, 2017 at 4:59:33 PM UTC-4, Colin Fleming wrote:
Object doesn't have a getName() method.

This doc is confusing - as Phill comments above, this is calling the getName() method on an instance of Class. In Clojure, a bare classname (String, ArrayList or whatever) resolves to the class itself if it has been imported (i.e. what would be String.class in Java). The doc is confusing because (.instanceMember Classname args*) is really just a special case of (.instanceMember instance args*), where "instance" refers to an instance of a Class object. I'm not sure why that doc makes that distinction, it doesn't seem useful to me.

Oh. I see. `SomeClass` is an instance of java.lang.Class.

    user=> (. (class String) getName)
    "java.lang.Class"

Though, the second code block down at <https://clojure.org/reference/java_interop> says:

    (.instanceMember Classname args*) ==>
        (. (identity Classname) instanceMember args*)

However, I don't understand why this works:

    user=> (. (identity String) getName)
    "java.lang.String"

since

    user=> (= String (identity String))
    true

and this fails:

    user=> (. String getName)
    CompilerException java.lang.NoSuchFieldException: getName, compiling:(/tmp/form-init2724986764275224936.clj:1:1)


John Gabriele

unread,
Mar 21, 2017, 12:43:25 PM3/21/17
to Clojure
On Tuesday, March 21, 2017 at 12:35:05 PM UTC-4, John Gabriele wrote:
On Monday, March 20, 2017 at 4:59:33 PM UTC-4, Colin Fleming wrote:
Object doesn't have a getName() method.

This doc is confusing - as Phill comments above, this is calling the getName() method on an instance of Class. In Clojure, a bare classname (String, ArrayList or whatever) resolves to the class itself if it has been imported (i.e. what would be String.class in Java). The doc is confusing because (.instanceMember Classname args*) is really just a special case of (.instanceMember instance args*), where "instance" refers to an instance of a Class object. I'm not sure why that doc makes that distinction, it doesn't seem useful to me.

Oh. I see. `SomeClass` is an instance of java.lang.Class.

    user=> (. (class String) getName)
    "java.lang.Class"


Erf. Sorry. I don't think I understand that after all, and as well may have confused java.lang.String and java.lang.Class in my above reply.

I also notice now that:

    user=> (class String)
    java.lang.Class

but

    user=> (. java.lang.Class getName)

Alex Miller

unread,
Mar 21, 2017, 5:53:10 PM3/21/17
to Clojure
On further reflection, I've decided not to change this. My comment here: https://github.com/clojure/clojure-site/issues/171#issuecomment-288230603 - further discussion related to this is probably best there on the issue for posterity.

Alex Miller

unread,
Mar 21, 2017, 5:59:28 PM3/21/17
to Clojure


On Tuesday, March 21, 2017 at 11:43:25 AM UTC-5, John Gabriele wrote:

Erf. Sorry. I don't think I understand that after all, and as well may have confused java.lang.String and java.lang.Class in my above reply.

I also notice now that:

    user=> (class String)
    java.lang.Class

but

    user=> (. java.lang.Class getName)
    CompilerException java.lang.NoSuchFieldException: getName, compiling:(/tmp/form-init2724986764275224936.clj:1:1)


This is exactly the reason that it's actually a different case. As the reference doc says, the equivalent expansion is:

user=> (. (identity Class) getName)
"java.lang.Class"


Colin Fleming

unread,
Mar 21, 2017, 6:52:40 PM3/21/17
to clo...@googlegroups.com
I commented over there too - I'm still confused.

Reply all
Reply to author
Forward
0 new messages