Clojure JIRAhttp://dev.clojure.org/jira
This file is an XML representation of an issueen-us4.464925-07-2011[CLJ-1138] data-reader returning nil causes exceptionhttp://dev.clojure.org/jira/browse/CLJ-1138
Clojure<p>If a data-reader returns nil, the reader throws java.lang.RuntimeException: No dispatch macro... The error message implies that there is no dispatch macro for whatever the first character of the tag happens to be.</p>
<p>Here's a simple example:</p>
<p> (binding <span class="error">&#91;*data-readers* {'f/ignore (constantly nil)}&#93;</span> (read-string "#f/ignore 42 10"))</p>
<p>RuntimeException No dispatch macro for: f clojure.lang.Util.runtimeException (Util.java:219)</p>clojure 1.5 beta2, Mac OS X 10.8.2, java version &quot;1.6.0_37&quot;CLJ-1138data-reader returning nil causes exceptionDefectMinorOpenUnresolvedUnassignedSteve MinerreaderSat, 22 Dec 2012 08:41:18 -0600Wed, 22 Jul 2015 07:58:16 -0500Release 1.4Release 1.5Release 1.6Release 1.7Release 1.812<p>clj-1138-allow-data-reader-to-return-nil-instead-of-throwing.patch allows a data-reader to return nil instead of throwing. Does sanity check that possible tag or record isJavaIdentifierStart(). Gives better error message for special characters that might actually be dispatch macros (rather than assuming it's a tagged literal).</p><p>clj-1138-data-reader-return-nil-for-no-op.patch allows a data-reader returning nil to be treated as a no-op by the reader (like #_). nil is not normally a useful value (actually it causes an exception in Clojure 1.4 through 1.5 beta2) for a data-reader to return. With this patch, one could get something like a conditional feature reader using data-readers.</p><p>clj-1138-allow-data-reader-to-return-nil-instead-of-throwing.patch is the first patch to consider. It merely allows nil as a value from a data-reader and returns nil as the final value. I think it does what was originally intended for dispatch macros, and gives a better error message in many cases (mostly typos).</p>
<p>The second patch, clj-1138-data-reader-return-nil-for-no-op.patch, depends on the other being applied first. It takes an extra step to treat a nil value returned from a data-reader as a no-op for the reader (like #_).</p><p>It turns out that you can work around the original problem by having your data-reader return '(quote nil) instead of plain nil. That expression conveniently evaluates to nil so you can get a nil if necessary. This also works after applying the patches so there's still a way to return nil if you really want it.</p>
<p> (binding <span class="error">&#91;*data-readers* {'x/nil (constantly '(quote nil))}&#93;</span> (read-string "#x/nil 42"))<br/>
;=&gt; (quote nil)</p><p>Patch clj-1138-allow-data-reader-to-return-nil-instead-of-throwing.patch dated Dec 22 2012 still applies cleanly to latest master if you use the following command:</p>
<p>% git am --keep-cr -s --ignore-whitespace &lt; clj-1138-allow-data-reader-to-return-nil-instead-of-throwing.patch</p>
<p>Without the --ignore-whitespace option, the patch fails only because some whitespace was changed in Clojure master recently.</p><p>OK, now with latest master (1.5.0-RC15 at this time), patch clj-1138-allow-data-reader-to-return-nil-instead-of-throwing.patch no longer applies cleanly, not even using --ignore-whitespace in the 'git am' command given above. Steve, if you could see what needs to be updated, that would be great. Using the patch command as suggested in the "Updating stale patches" section of <a href="http://dev.clojure.org/display/design/JIRA+workflow">http://dev.clojure.org/display/design/JIRA+workflow</a> wasn't enough, so it should probably be carefully examined by hand to see what needs updating.</p><p>I removed my patches. Things have changes recently with the LispReader and new EdnReader.</p>Global RankPatchCode and Test