Getting Closures: The Groovy Way

As I mentioned in my last Groovy post I was going to try and get my head around the concept of Closures, it's not that it is a hard concept it's just different.

It's not like I haven't seen closures before, I've seen them quite often in Javascript as well as in Actionscript 3.0 but it's just that with groovy it seems to have been taken to the next level.

So What is a Closure?

If you followed the above link to wikipedia then you can skip over what I am about to write here as it will not be as in depth and hell I may even get it wrong!

A closure is a chunk of code that is wrapped up as an object. It's a method and an object at the same time, it can accept parameters and you can pass a reference to it around like you can with a normal object.

Clear as mud right? How about an example? Javascript perhaps?


<script>
function sayHello(name) {
var text = 'Hello ' name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}
</script>

If you have worked with any of the Javascript UI frameworks such as EXT JS closures will probably be quite familiar to you. If you have done any Actionscript 3 programming closures will also be quite familiar to you too. Here is an Actionscript 3 example.


private function doInit(evt:Event) : void {
btn.addEventListener('click', function(evt:Event):void {
Alert.show( 'hello' txtName.text);
});

And finally something in Groovy



new File('c:/myfile.txt').eachLine { println it }

In the above Groovy code I open the file myfile the method eachLine loops over each line in the file (bet you had trouble guessing that!) and the closure {println it} prints out each line using the it magic variable to the console.

Tip of the Iceberg

So that was just a taste of what closures are, but that's just the tip of the iceberg things get very interesting very fast once you start looking at method closures and multimethod closures (think overloaded methods in a class).

An example from Groovy in Action (reprinted without permission):


class MultiMethodSample {
    int mysteryMethod (String value) {
    return value.length()
    }
    int mysteryMethod (List list) {
    return list.size()
    }
    int mysteryMethod (int x, int y) {
    return x+y
    }
    }
    MultiMethodSample instance = new MultiMethodSample()
    Closure multi = instance.&mysteryMethod
    println multi ('string arg')
    println (['list', 'of', 'values'])
    println multi (6, 8)

I think Chapter five is one chapter that I can't just skim over.

Happy Coding...

Related Blog Entries

0 Comments to "Getting Closures: The Groovy Way"- Add Yours

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui

Recent Entries

No recent entries.

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List