On Mar 7, 2010, at 10:57 PM, CuppoJava wrote:

> Is there an elegant solution to this problem? I'm working around it by
> saving the original println in another variable before creating
> myprintln, but this isn't very clean.

In case by another "variable", you were referring to another "var":

One relatively clean way to capture the old value is in a closure:

        user=> (let [println println]
                 (defn myprintln [str]
                   (println str)
                   (println "tacked on")))
        #'user/myprintln

This doesn't work in the current head of Clojure because by default, calls to 
functions bound to vars in namespaces whose names begin with "clojure" (e.g., 
clojure.core) are inlined by the compiler. Binding the var has no effect on how 
the compiled code runs. My understanding is that this is an experimental 
optimization that Rich is trying out.

Wrapping println in a bindable wrapper (in the user namespace) proves the 
concept:

        user=> (defn _println [x] (println x))
        #'user/_println
        user=> (let [_println _println]
                 (defn myprintln [str]
                   (_println str)
                   (_println "tacked on")))
        #'user/myprintln
        user=> (binding [_println myprintln] (_println "Some code"))
        Some code
        tacked on
        nil
        user=>

--Steve

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to