2012-07-21

izard: (Default)
2012-07-21 11:53 am
Entry tags:

Debugging in clojure

Finding a bug in my code in 90% of the cases has two phases:
1. This part of the code I wrote is correct, this must be compiler/library bug!
2. I think "ohh, how stupid was I writing that in the first place. This is so trivial and is of course a my fault!"

=> (map buggy-fn array)
< classcastexception java.lang.Long cannot be cast to clojure.lang.IFn >
=> (count array)
2
=> (buggy-fn (first array))
()
=> (buggy-fn (second array))
()
; Let's try contrib.trace! Never done that...
=> (defn reproduce [array] (map buggy-fn array))
=> (dotrace [reproduce] (reproduce))
# < illegalstateexception dynamically="dynamically" bind="bind" non-dynamic="non-dynamic" var:="var:" >
; Ooops - does not work, will have to resort to debug/dbg output traces as usual.


From the session above, looks like something really wrong is happening with clojure, it can't be that calling a pure function from map is different from calling it with explicit arguments. However I am 90% sure that after I debug the buggy-fn, I will find my coding error.

Upd: Found it, the function buggy-fn is not pure, so it was a side effect.