Skip to content →

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.

Published in Clojure

2 Comments

  1. Cool, I didn’t know about this. This will be handy. Clojure’s stack traces in SLIME are confusing and verbose enough that I’ll take all the help I can get. clj-backtrace doesn’t seem to work too well for me.

  2. This is a great tip for dealing with Clojure’s multimethods. One thing that I’ve found tough about Clojure are the sometimes cryptic compiler messages. Thanks for the info!

Leave a Reply

Your email address will not be published. Required fields are marked *