On Fri, Sep 9, 2011 at 7:47 AM, Tassilo Horn <tass...@member.fsf.org> wrote:
> Hi all,
>
> I wanted to create some stand-alone example that demonstrates my issue
> from the other thread: Message-ID: <8762l2n391....@thinkpad.tsdh.de>
>
> While trying to fiddle something together, I became aware that any code
> inside #=(..) that includes java interop constructs fail.

The #= syntax is intentionally undocumented, which means it's hard to
know what is and is not supported, or how it's supposed to work.  I
don't know if it's possible to call instance methods, but static
methods and constructors work fine using the "modern" syntax.

> user> (new java.util.Date 1 2 3)
> #<Date Sun Mar 03 00:00:00 CET 1901>
> user> #=(new java.util.Date 1 2 3)
> ; Evaluation aborted.
> Can't resolve new

Use #=(java.util.Date. 1 2 3) instead.

> user> (.newInstance java.util.Date)
> #<Date Fri Sep 09 13:25:09 CEST 2011>
> user> #=(.newInstance java.util.Date)
> ; Evaluation aborted.
> Can't resolve .newInstance

This is an instance method call.  As I said above, I don't know if
this can be made to work at all in #=()

> Hm, but even without the reader #=(..) syntax, there's some strangeness:
>
> --8<---------------cut here---------------start------------->8---
> user> (. java.util.Date newInstance)
> ; Evaluation aborted.
> java.lang.NoSuchFieldException: newInstance
>  [Thrown class java.lang.RuntimeException]
> user> (. (identity java.util.Date) newInstance)
> Reflection warning, NO_SOURCE_FILE:1 - reference to field newInstance can't 
> be resolved.
> #<Date Fri Sep 09 13:46:06 CEST 2011>
> --8<---------------cut here---------------end--------------->8---
>
> Why do I have to use identity here?

This used to be Clojure FAQ #1, when no other syntax was available.
newInstance is an instance method of the class Class, so these days
the best way to call it is (.newInstance java.util.Date).  If you want
a static method of class Date, do something like (java.util.Date/UTC 1
2 3 4 5 6).  These are "modern" syntax.

It's generally best not to use the older form (. foo bar) as it can be
ambiguous.  But just so you know what's going on... If Clojure finds
that foo is a class name, it assumes that bar is a static method or
field of class bar.  When if can't find a foo in bar, it gives you the
error above.  By wrapping foo in 'identity' you're making it clear to
the compiler that the first arg to . is an expression not a class
name, so it then looks for an instance method or field.

--Chouser
http://joyofclojure.com/

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to