Sorry to dig up such an old thread. I'd also like to maintain the bindings of dynamic vars across asynchronous function calls.
Is there a workaround that people use in the absence of bound-fn, etc? Cheers, Stuart On Friday, 27 January 2012 16:49:10 UTC+11, Brandon Bloom wrote: > > The ClojureScript > wiki<https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure>states > that "the user experience of [binding] is similar to that in > Clojure" but my very first experiment produced wildly different results > between platforms. > > Here's a Clojure on the JVM session: > > user=> (import java.lang.Thread) > java.lang.Thread > user=> (defn set-timeout [ms fn] (.run (Thread. #(do (Thread/sleep ms) > (fn))))) > #'user/set-timeout > user=> (def x "top level") > #'user/x > user=> (binding [x "in binding"] (println x) (set-timeout 1000 #(println > x))) > in binding > in binding > nil > > And here's the analogous ClojureScript session: > > ClojureScript:cljs.user> (def x "top level") > "top level" > ClojureScript:cljs.user> (binding [x "in binding"] (println x) > (js/setTimeout #(println x) 1000)) > in binding > 21 > top level > > So ignoring the sequencing and nil vs timeout-id return values, the > binding of 'x wasn't preserved in the asynchronous callback. > > I raised this issue in #clojure and @dnolen said that "that's the behavior > there's nothing much to fix", but that didn't sit right with me. This seems > like either 'binding is bugged, or maybe I don't understand something about > its intent. > > On the topic of "Vars" proper, I understand their usefulness in > repl-centric development, where you can redefine functions at runtime. The > wiki also makes some mention of this, but I can't wrap my head around the > context and jargon. I've run into this problem before in Javascript, where > some level of indirection is necessary to support run-time redefinitions. > You can't do `var fn = package.fn;` and dynamically redefine `fn` from > `package` later because a copy of the reference is made. How does > ClojureScript address this problem? > > Cheers, > Brandon > -- 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