Dear Java, I need closure....

Published: 11:11 PM GMT+12, Thursday, 25 November 2004 under: technology smalltalk

I'm sitting here having on of those "enlightened" moments when you suddenly grok the power of something, in this case its closures/blocks. I've been loving the power and flexability Smalltalk's blocks provide for sometime now, and whilst Java's anonymous inner classes provide some similar functionality, there hardly as clean.

I had some code which, when let loose to a customer suddenly started to crawl ( reading through large distributed global address lists from Outlook, via its (slow) object model ), the task was to add a progress display around the process - initially my mind went "eeep" and cowering in fear at having to grok Dolphin's threading models and how they intermix with Windows threads, but then I had a memory "wait, I've seen funky progress bars here before, maybe someones already done the dirty for me" - so I open the class browser and search for "ProgressB" and up pops the ProgressDialog class, a quick scan through the methods and I spot example1, example2, and example3 on the class side... *blink* - this looks easy....

So I set about adding it to my code, and dayam - this is freaken easy!

(ProgressDialog operation: [ :progressHandler |
"My original long lasting looping code goes here."
......
progressHandler value: somePercentageValue.
......
]) caption: 'Please wait whilst I do something really slow...'; show.

The operation message takes a block, and also passes it a block. The block thats passed to your block simply updates the display, no worrying about threads anywhere ( I'm guessing that's handled behind the scenes somewhere ). The power here is that I'm not having to worry about nasty declared interfaces, methods, and final variables that Java would have to write, i.e.

ProgressDialog.operation( new Runnable() { public void run() {..... }});

Instead, I get code that looks, no - IS identical to my original code, only its indented, with 2 lines of extra code around it - I like that.

Ironically, it's also taken me 3-4 times longer to write this blog post than it was to find the class, read how ProgressDialog worked, implement it, and run my code...

Comments (5)

Why wait for Groovy? Scheme is in production already in the JVM.

SISC (http://sisc.sourceforge.net) is a complete and efficient Scheme.

JScheme (http://jscheme.sourceforge.net/jscheme/main.html) is less complete and efficient, but simpler and useful for its integration with Java.

Patrick Logan [patrickdlogan@stardecisions.com]

left by Anonymous User . Sunday, 5 December 2004 5:52 AM

Blocks / Closures are nice. And as a result of Java5's auto-boxing support we may actually see them at some point.

The real feature I want important from smalltalk/lisp is continuations. Heck, even a simplified continuation syntax in the ala generators from python.

Brett Morgan [brett@uts.edu.au]

left by Anonymous User . Friday, 26 November 2004 3:06 PM

May we have permission to reprint your blog at sys-con.com/java over the (US) Thanksgiving holiday weekend - which starts today! ;-)

We try and do this from time to time, and the result is usually a boost of feedback that you mightn't otherwise have gotten.

We'd add your byline and bio, obviously, so ping me those when you get a moment, and I'll ping you back with a preview URl.

Thanks in advance,

Jeremy G.

--
Jeremy Geelan
Group Publisher, SYS-CON Media
http://sys-con.com

email: jeremy@sys-con.com

Web Services Edge 2005 East - International Web Services Conference & Expo
Hynes Convention Center, Boston, MA - February 15 - 17, 2005

Tuesday -> 2/15, Conference & Expo
Wednesday -> 2/16, Conference & Expo
Thursday -> 2/17, Conference & Expo

http://sys-con.com/edge

jeremy geelan [jeremy@sys-con.com]

left by Anonymous User . Friday, 26 November 2004 1:38 AM

Hey Alan, yes - Groovy does look promising ( as well as Nice ( http://nice.sourceforge.net/ ). I'm definitely keep an eye on them both...

left by Mark Derricutt . Thursday, 25 November 2004 3:17 PM

You're gonna love Groovy, if it ever gets stable :)

Alan Green [alan.green@cardboard.nu]

left by Anonymous User . Thursday, 25 November 2004 3:11 PM
Add Comment