On Thu, Jul 7, 2011 at 11:21 PM, Ken Wesson <[email protected]> wrote:
> Interesting, but there is still going to be a performance issue for
> the perhaps-common case of testing a non-seqable for seqability: in
> that case, it will do the reflective check for isArray and it won't
> use the protocol to cache the result. Making it do so, however, would
> cause problems if one had (seqable? some-foo) and later attempted to
> extend the protocol to mark Foos as seqable within the same runtime
> session.
>
> I'm surprised that (.isArray (.getClass foo)) is that slow, though. It
> may be "reflection", but it's not the sort I was given to understand
> was slow, namely reflective method invocation. An obvious JVM
> implementation would make the first word of every object a pointer to
> its concrete Class instance and then the above should be no worse than
> (.length (.getSomeString some-bean)): a method call and a field lookup
> in an object whose pointer was the result of another method call and
> another field lookup in another object. Indeed, both method calls are
> nonpolymorphic in the former case, since the getClass method is final
> and the entire Class class is final so isArray is also final.
(extend-type Object
ISeqable
(seqable? [x]
(let [c (.getClass x)]
(if (.isArray c)
(do
(extend-type (.getClass x)
ISeqable
(seqable? [x] true))
true)
(do
(extend-type (.getClass x)
ISeqable
(seqable? [x] false))
false)))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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