Type hint Java array of your own Java class

975 views
Skip to first unread message

David Nolen

unread,
Feb 4, 2009, 12:29:19 PM2/4/09
to clo...@googlegroups.com
(defn foobar [#^MyClass[] myarray])

This syntax doesn't seem to work.

Christophe Grand

unread,
Feb 4, 2009, 12:49:13 PM2/4/09
to clo...@googlegroups.com
David Nolen a écrit :

> (defn foobar [#^MyClass[] myarray])
>
> This syntax doesn't seem to work.
Indeed it's a bit tricky:
#^"[Lyour.package.YourClass;"

You can find it by doing:
user=> (class (into-array [(your.package.YourClass.)]))
[Lyour.package.YourClass;

Christophe

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)


David Nolen

unread,
Feb 4, 2009, 1:49:28 PM2/4/09
to clo...@googlegroups.com
Many thanks.

Chouser

unread,
Feb 4, 2009, 4:55:03 PM2/4/09
to clo...@googlegroups.com
On Wed, Feb 4, 2009 at 12:49 PM, Christophe Grand <chris...@cgrand.net> wrote:
>
> David Nolen a écrit :
>> (defn foobar [#^MyClass[] myarray])
>>
>> This syntax doesn't seem to work.
> Indeed it's a bit tricky:
> #^"[Lyour.package.YourClass;"
>
> You can find it by doing:
> user=> (class (into-array [(your.package.YourClass.)]))
> [Lyour.package.YourClass;

Christophe, thanks for pointing the way. Sorry I kept going...

(defn array-of [cname]
(-> cname resolve .newInstance list into-array class .getName))

With that, you can:

(defn foobar [#^#=(array-of MyClass) myarray])

At least it appears to work:

user=> (meta #^#=(array-of String) [])
{:tag "[Ljava.lang.String;"}

Again, I apologize for even suggesting this.

--Chouser

chris

unread,
Feb 4, 2009, 6:22:49 PM2/4/09
to Clojure
Sorry for jumping in, but

"#^#=" doesn't make any sense to me. Where did this come from?

Chris

On Feb 4, 2:55 pm, Chouser <chou...@gmail.com> wrote:

Chouser

unread,
Feb 4, 2009, 9:24:44 PM2/4/09
to clo...@googlegroups.com
On Wed, Feb 4, 2009 at 6:22 PM, chris <cnue...@gmail.com> wrote:
>
> "#^#=" doesn't make any sense to me. Where did this come from?

I already apologized twice, what more do you want from me!?

As penance, I will try to explain.

#^ is a reader macro, documented at http://clojure.org/reader

#= is another reader macro, largely undocumented, but discussed here:
http://groups.google.com/group/clojure/browse_thread/thread/7a24eeb982490a87/d579d881fea5b16d?#d579d881fea5b16d

>> user=> (meta #^#=(array-of String) [])
>> {:tag "[Ljava.lang.String;"}

The whole point of this example was to attach type-hint metadata to a
function arg, so the use of #^ was necessary. But usually #^ just
takes a literal symbol or map, and I wanted to use the results of a
function call, so I abused #= to do just that.

--Chouser

Christophe Grand

unread,
Feb 5, 2009, 1:33:35 AM2/5/09
to clo...@googlegroups.com
Chouser a écrit :

> (defn foobar [#^#=(array-of MyClass) myarray])
>
> Again, I apologize for even suggesting this
Wow what a clever (ab)use of reader macros!

Christian Vest Hansen

unread,
Feb 5, 2009, 4:20:11 AM2/5/09
to clo...@googlegroups.com
On Thu, Feb 5, 2009 at 7:33 AM, Christophe Grand <chris...@cgrand.net> wrote:
>
> Chouser a écrit :
>> (defn foobar [#^#=(array-of MyClass) myarray])
>>
>> Again, I apologize for even suggesting this
> Wow what a clever (ab)use of reader macros!

This is where a raptor jumps in from the left and eats someone :)

>
> --
> Professional: http://cgrand.net/ (fr)
> On Clojure: http://clj-me.blogspot.com/ (en)
>
>
>
> >
>



--
Venlig hilsen / Kind regards,
Christian Vest Hansen.
Reply all
Reply to author
Forward
0 new messages