On Fri, Nov 20, 2009 at 8:02 PM, Mark Engelberg <mark.engelb...@gmail.com> wrote: > Dynamic binding is confusing, and should be used with care.
Elaborating on my previous point: it's especially confusing because dynamic binding interacts poorly with lazy data structures, which is predominantly what you deal with in Clojure. If your lazy data structure depends on something that is dynamically bound using binding, there is a good chance it won't get evaluated until after you exit the scope of the binding and the global variable is restored to its earlier state, so your lazy data structure ends up not using the value that was set with binding. It can get messy fast. There were a couple times I used dynamic binding, and I was really excited about it. I had never used a language with a dynamic binding construct, and it seemed like a perfect fit for what I was doing. I was threading a couple of values through a bunch of mutually recursive functions that generated a tree. The values I was threading controlled certain aspects of the tree generation. I realized that by making these generation parameters global values, and just wrapping binding to set the parameters around the call to my tree generator, I could avoid all these extra inputs on a whole slew of functions that were just passing these parameters around so they'd be available in the functions that really needed them. But my tree generator used laziness in some non-obvious places, and it took me a long time to notice that when I didn't fully evaluate the tree right away, I was getting very different results. It was hard to track down the problem, and ultimately I had to go back to threading all the parameters through the functions explicitly. The experience really turned me off of dynamic binding in Clojure, since it has such a strong laziness focus. Which reminds me, every once in a while I see people talking about this here, and brainstorming up some alternatives to binding that might interact better with lazy data structures. Has there been any real progress on this, or has every proposed solution been equally problematic? -- 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