(use '[clojure.contrib.trace])
(dotrace [str] (reduce str [\a \b]))
(use ‘[clojure.contrib.trace]) (dotrace [str] (reduce str [\a \b]))
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a nutshell:
That’s because
trace-fn-call, which is the thingdotraceuses to wrap the functions to be traced, usesstrto produce the niceTRACE foo => valoutput.Extended explanation:
The
dotracemacro does its magic by installing a thread binding for each Var holding a function to be traced; in this case, there is one such Var,clojure.core/str. The replacement looks roughly like so:The
trace-fn-call, to quote its docstring, “Traces a single call to a function f with args.”. In doing so, it calls the traced function, takes note of the return value, prints out a nice informative message of the formTRACE foo => valand returns the value obtained from the traced function so that regular execution may continue.As mentioned above, this
TRACE foo => valmessage is produced usedstr; however, in the case at hand, this is actually the function being traced, so a call to it leads to another call totrace-fn-call, which makes its own attempt to produce the tracing output string usingstr, which leads to another call totrace-fn-call… ultimately leading to the stack blowing up.A workaround:
The following modified versions of
dotraceandtrace-fn-callshould work fine even in the presence of weird bindings for core Vars (note that futures may not be scheduled promptly; if that’s a problem, see below):(Rebinding
trace-fn-callaround a regulardotraceapparently doesn’t work; my guess is that’s because ofclojure.*Var calls still being hard-wired by the compiler, but that’s a separate matter. The above will work, anyway.)An alternative would be to use the above
my-dotracemacro together with amy-trace-fn-callfunction not using futures, but modified to call custom replacements for theclojure.contrib.tracefunctions using the following in place ofstr:The replacements are straightforward and tedious and I omit them from the answer.