Monday, July 16, 2012

Dynamic bindings vs. laziness in Clojure

Now here's a thing that cost me quite a while to figure out: dynamic variables easily get in your way when accessed from lazy expressions. What happened to me is that a lazy expression using a dynamic var wouldn't remember the value of the dynamic var after leaving its binding form before the lazy expression was actually evaluated. It is only a slight relief that people more skilled than I have run into that problem, too. 

So I guess a bunch of things in the language are not exactly simple.

1 comment:

  1. Same is true for elisp ;) lexical-bind to the rescue. Anyway, for certain problems dynamic scope is much more convenient, e.g. when using buffer-bound modes in emacs that determine certain behaviors / state. In that sense dynamic layer activation in COP is similarly used.

    ReplyDelete