I thought I'd share an observation with the group.
I'm walking through my fork of Dan Larkin and Phil Hagelberg's HTTP
client library, fixing reflection warnings and generally seeing if I
can add some features (to bring it up to parity with some code I've
written that uses the Apache HTTP libraries -- proxy settings being
one example).
One such warning comes from a use of as-str, in clojure.contrib.java-
utils. This function is
(defn as-str
"Returns the name or string representation of x"
[x]
(if (instance? clojure.lang.Named x)
(name x)
(str x)))
Obviously (to a human) this always returns a string, but as-str's
return type is not labeled as such, and so
(set! *warn-on-reflection* true)
(use 'clojure.contrib.java-utils)
(defn foo [x]
(.toLowerCase (as-str x)))
will produce a warning that toLowerCase can't be resolved. Presumably
the generated code will incur a cost for reflection, turning a trivial
utility function into an unexpectedly expensive operation.
It strikes me that it's somewhat ugly to force users of libraries to
settle for reflection, write code like
(defn foo [x]
(let [#^String s (as-str x)]
(.toLowerCase s)))
or
(defn foo [x]
(.toLowerCase #^String (as-str x)))
or -- worse -- end up rewriting library code to avoid the need for
such annotations.
I expect that static analysis could avoid the need for explicit
annotations in many cases, but Sufficiently Smart Compilers take time,
and I don't expect one any time soon.
Perhaps we should all try to ensure that -- wherever practical --
straightforward uses of our libraries' public interfaces don't incur
reflection warnings? Please don't interpret this as a complaint: I
simply feel that publishing libraries (particularly in contrib)
entails a duty to do things "clean and right", and maybe folks hadn't
thought about this issue yet.
Thoughts? I would have no trouble simply issuing pull requests to add
type annotations to contrib libraries, but I would feel a bit silly
opening issues and generating manual patch files for the odd #^String.
-R
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---