On May 11, 2:20 pm, Vincent Akkermans <[email protected]>
wrote:
> Hi all,
>
> I'm building an application in which I want some function calls to be
> logged. However, when I have function A and B (both will be logged)
> and A is called from B, I want the log to show this relation. I
> defined a small macro for this purpose:
>
> (def *hierarchy* [])
> (def *action-id* nil)
>
> (defn action-id []
>   (if (nil? *action-id*)
>     (gensym "action-")
>     *action-id*))
>
> (defmacro defalgo [name args & body]
>   `(defn ~name ~args
>      (do (binding [*hierarchy* (conj *hierarchy* (str ~name))
>                    *action-id* (action-id)]
>            (log *action-id* *hierarchy*)
>            ~...@body))))
>
> Any top level calls of functions defined with defalgo will be logged
> with a different id because of the gensym. Functions called from
> within the toplevel call will be logged with the same id as the
> toplevel call. This all works perfectly fine. Why post this then?
>
> I was testing this mechanism with a loop to simulate the recursive
> bindings and I got some unexpected behavior.
>
> (defn action-id []
>   (if (nil? *action-id*)
>     (gensym "action-")
>     *action-id*))
>
> (defn action-id-test []
>   (loop [counter 0]
>     (binding [*action-id* (action-id)]
>       (println *action-id*)
>       (if (< counter 3)
>         (recur (inc counter))
>         nil))))
>
> Executing from the repl gave this result:
>
> nl.tbb.cometucoco=> *action-id*
> nil
> nl.tbb.cometucoco=> (action-id-test)
> action-5177
> action-5177
> action-5177
> action-5177
> nil
> nl.tbb.cometucoco=> *action-id*
> action-5177
> nl.tbb.cometucoco=> (set! *action-id* nil)
> nil
> nl.tbb.cometucoco=> (action-id-test)
> action-5184
> action-5184
> action-5184
> action-5184
> nil
> nl.tbb.cometucoco=> *action-id*
> action-5184
>
> *action-id* isn't rebound to its initial value. This only happens when
> the binding occurs inside the loop. This isn't the expected behavior
> right? Why is *action-id* sticky??
>

Known issue:

http://code.google.com/p/clojure/issues/detail?id=27

Rich

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to