Viewing By Entry / Main
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>
   <cftransaction>
      <cfset arguments.block.call() />
   </cftransaction>
   <p>End Transaction!</p>
</cffunction>

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

<cfset operation = cf.new(dbCode) />

<p>Naked operation:</p>
<cfset operation.call() />

<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?

Comments

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. =)


Thank you Sean - yes the bulb is beginning to dimly flicker.


@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.


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:

http://www.bennadel.com/index.cfm?dax=blog:585.view

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 :)


@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 "get it" but make almost no sense until then... :)


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?


@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...


Post Your Comments
Name:
Email Address:
Comments
*** Please note that all comments require moderation so it may be some time before your comment posts to this blog! ***
Remember My Information:
 



Hosting provided by