(clojure.java.io/resource "no-such-file")

171 views
Skip to first unread message

Elric Erkose

unread,
Jun 3, 2015, 3:48:15 PM6/3/15
to clo...@googlegroups.com
The doc string for clojure.java.io/resource states that it returns a URL. When an invalid file is passed as the only parameter, it returns nil. Shouldn't we expect an Exception?

user=> (clojure.java.io/resource "no-such-file")
nil

Andy Fingerhut

unread,
Jun 3, 2015, 4:02:58 PM6/3/15
to clo...@googlegroups.com
Unofficial answer from the sidelines.  I am in no way associated with the folks who make decisions like this, other than that some of them know my name.

Throwing an exception in that case sounds like reasonable behavior to me, but of course any code written now that uses clojure.java.io/resource and relies on it returning nil, and does not catch an exception, could break if clojure.java.io/resource changed behavior from one Clojure release to the next.  That is not to say that it should not change, or will not change, but a significant factor to consider in whether it will change.  Anyone is welcome to create a ticket in JIRA requesting changes, just to get the question/suggestion 'in the system' and see what the response is (which may take significant time, given the other work desired to be done, some of which may be considered higher priority).

I am probably belaboring the obvious by noting that if you want a function that behaves as you describe, it is pretty easy to write one using the existing clojure.java.io/resource.  Please, pretty please, do not take this statement as a dismissal of your question.  It is not intended that way.

Andy

--
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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Reeves

unread,
Jun 3, 2015, 4:17:00 PM6/3/15
to clo...@googlegroups.com
I'd say this was consistent with Clojure's other lookup functions. When we lookup a value in a data structure that doesn't exist, nil is returned by default, rather than an exception.

- James

Elric Erkose

unread,
Jun 3, 2015, 4:40:02 PM6/3/15
to clo...@googlegroups.com, ja...@booleanknot.com
@james Sounds reasonable, though it may not be consistent (or I'm missing your point).

user=> (get {:a 1} :no-such-file)
nil
user=> (get {:a 1} nil)
nil


Compared with

user=> (clojure.java.io/resource "no-such-file")
nil
user=> (clojure.java.io/resource nil)
NullPointerException   sun.misc.MetaIndex.mayContain (MetaIndex.java:242)

Matching Socks

unread,
Jun 3, 2015, 6:43:56 PM6/3/15
to clo...@googlegroups.com
The docstring is vague about the failure modes, but it drops a clue: "Use [sic] the context class loader
if no loader is specified."  The Javadoc for ClassLoader.getResource says it returns "null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource".

Currently, clojure.java.io/resource is convenient for use with "some", "filter", and "if".  A Jira ticket suggesting a more explicit docstring would be helpful.


Alex Miller

unread,
Jun 3, 2015, 7:51:16 PM6/3/15
to clo...@googlegroups.com
Without reading or thinking about this deeply, I did want to mention the existing ticket http://dev.clojure.org/jira/browse/CLJ-1210 which seems like it's in the ballpark of this.

Elric Erkose

unread,
Jun 3, 2015, 9:20:43 PM6/3/15
to clo...@googlegroups.com
This is what I ran into with (slurp nil) <actually> (slurp (clojure.java.io/resource "some-file"))
Reply all
Reply to author
Forward
0 new messages