CFML, Clojure, Software Design, Frameworks and more...

Sammy Larbi on Closures

January 2, 2007 ·

Thanx to Brian Rinaldi's Open Source Update, I found a new blog - Sammy Larbi's. Sammy tried out my Closures library and shows an example that sort of works so I figured I'd post the "right" way to do things so that a variant of Sammy's example works as he expects.
First off, Sammy mentions the fix he had to apply to make the Closures library work on his system. I thought it was a 6.1 / 7.0 difference but it turns out he found a real bug and the fix is needed even on 7.0 if you use a mapping for the library. Secondly, while debugging Sammy's example, I found another bug - a potential thread safety issue. Both are fixed in SVN and the latest "daily" build ZIP file.
So, moving on to Sammy's code, here's the corrected version of his container.cfc:

// iterates over the container, letting a closure // specify what to do at each iteration
function each(closure)
{
for (i=1; i lte _curIndex; i=i+1)
{
closure.call(_arr[i]);
}
}</cfscript></cfcomponent>

Compare his each() method to mine. He names the method and then repeatedly binds the value variable. That doesn't actually do what he thinks. If you want to call a closure, you simply call it with arguments as shown above.
However, in order to do that, you need to identify the arguments when you create the closure. You do that by specifying the argument names in the call to new() like this:

That fixes part of his problem.
His other problem is the beenhere variable. He wants to bind that variable to the context in which the closure is created. Note that he wants to update the variable inside the closure and output its (updated) value outside the closure. In order to do that, you need to bind the context. First we change the closure to this: