An Architect's View

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

An Architect's View

Closures Example

March 15, 2007 ·

When folks first look at my Closures for CFMX project, they often scratch their heads and go "Huh? What would you use that for?". I was reading an article on The Server Side which compares the possible syntax for three proposals that would introduce closures into Java. The syntax is mostly awful but, again, one of the problems was the lack of decent examples. One of the comments mentioned transaction wrapping and that seemed to be such a good example that would resonate with ColdFusion developers that I thought I'd show it sketched out here:
<cfset cf = createObject("component","closure.ClosureFactory") />

<cffunction name="withTransaction">
   <cfargument name="block" />
   <p>Begin Transaction!</p>
      <cfset />
   <p>End Transaction!</p>

<cffunction name="dbCode">
   <p>I'm a database operation!</p>

<cfset operation = />

<p>Naked operation:</p>
<cfset />

<p>Transacted operation:</p>
<cfset withTransaction( operation ) />
This produces the following output:
Naked operation:

I'm a database operation!

Transacted operation:

Begin Transaction!

I'm a database operation!

End Transaction!
The example doesn't show any bindings of variables but that would be where the real power comes in. Does that help show where closures can be useful?

Tags: coldfusion

7 responses

  • 1 Sammy Larbi // Mar 16, 2007 at 7:02 AM

    I think that's definitely another good example to have put out there.

    Someone might also think of it like what a dynamic cfinclude achieves for you in a template: You can specify the surrounding code while leaving the inner code for someone else to specify. That probably just muddied the water though. =)
  • 2 jim collins // Mar 16, 2007 at 8:57 AM

    Thank you Sean - yes the bulb is beginning to dimly flicker.
  • 3 Sean Corfield // Mar 16, 2007 at 9:27 AM

    @Sammy, maybe, but the key power of the closure is the ability to bind variables in that would not otherwise be available in its execution context.

    I could bind a data source variable to the dbCode closure, both independent of the original function and of the (later) execution context. That's pretty powerful.
  • 4 Ben Nadel // Mar 16, 2007 at 10:26 AM

    Sean, this Closure stuff is very cool to me. I am having a bit of trouble quite getting there. I want to be able to something as in Javascript, but I am finding the variable binding very difficult and hacky! I with there was a way to pass a variable-number of arguments to a method (that does NOT use named arguments).

    After reading the above, I threw this together:

    I think it's neat, but again, very hacky. When you have a few minutes, if you could take a peak and let me know if you have stuff built into your Closure stuff that could handle this much more elegantly.

    If you don't have time, that's cool, this stuff is FAR from time-sensitive :)
  • 5 Sean Corfield // Mar 16, 2007 at 12:20 PM

    @Ben, I've added a comment on your blog post. I hope it helps explain things?

    Closures are something that make a lot of sense once you &quot;get it&quot; but make almost no sense until then... :)
  • 6 Rob Cameron // Mar 16, 2007 at 1:11 PM

    This isn't something from Ruby that you envy, is it? ;) Speaking of Ruby, what ever came of that post you had not too long ago looking for combo Ruby/CF experts? Is Adobe making a RubyFusion server?
  • 7 Sean Corfield // Mar 16, 2007 at 1:28 PM

    @Rob, I used languages with closures many years before Ruby existed :)

    The call for folks with both Ruby and ColdFusion experience produced the information I was looking for and it was passed on to the folks who needed it...