Warning: ob_start(): non-static method wpGoogleAnalytics::get_links() should not be called statically in /home/w01fe/w01fe.com/wordpress/wp-content/plugins/wp-google-analytics/wp-google-analytics.php on line 259
A Blog. » Clojure

Archive for Clojure

Reversing the order of Clojure’s exception chains in SLIME

When debugging a complex Clojure project with SLIME, I would often find myself chasing chains of exceptions as much as 20 levels deep to get to the true cause of a problem. I finally tired of the “11111111111”-look-“0000000000” exercise, especially since in my experience the most useful stack traces are almost always the innermost ones.

Thus, I decided to modify swank-clojure to present exceptions in the opposite order: rather than starting with the caught exception and providing the option to examine its “Cause”, start with the innermost cause and allow examining its “Source”. In addition to providing the must useful stack traces up front, this also means that the first thing you see in the SLDB window is the real exception, not a mess of RuntimeExceptions.

For the interested, I posted the change on GitHub; it’s my first real change to anything SLIME, so take care if you decide to use it.


Dynamic type hints in Clojure macros

Sometimes, one wants to write a Clojure macro to assist in Java interoperability, where the object types are determined by the arguments to the macro. In this situation, the normal strategy of using a #^class type hint doesn’t work, since the class isn’t known at read time. Here’s a very contrived example, where the macro declare-first-char defines a function first-char, which calls the (.charAt % 0) method on its argument. The class for the type hint is passed as an argument to the macro.

user> (set! *warn-on-reflection* true)
user> (defmacro declare-first-char [cls]
	(let [x (gensym)]
	  `(defn first-char [~(with-meta x {:tag cls})] 
             (.charAt ~x 0))))
user> (declare-first-char String)
#'user/first-char ; No reflection warning!
user> (first-char "test")

The key here is the [~(with-meta x {:tag cls})] fragment, which replaces the normal [#^String x#] version that could be used if the type was known in advance.

Comments (1)

Quick tip for debugging & profiling Clojure code with multimethods

Debugging and profiling Clojure code with multimethods can be a pain, since methods show up as anonymous functions in stack traces and profile results. When you’re debugging you at least get a line number, but if you’ve loaded your code in a nonstandard way (i.e., with SLIME) or you’re using a profiler like YourKit, you don’t get this information. Then, the only clue you have to figure out what my.long.namespace$fn_12312 represents is context — who called it, and who it calls.

Similar issues exist for anonymous functions, which some time ago I realized you could give names by using (fn name [args] & body). What I didn’t realize is the same is true of multimethods. By simply using (defmethod multifn dispatch-val *name* [args] &body), you can give each instance of a multimethod a unique name, which will show up in stack traces and profile results.

Comments (2)

« Previous entries Next Page » Next Page »