Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Closures in Java – catching up with Smalltalk?

Posted by Piotr Gabryanczyk on March 31, 2007

I was recently very happy to discover that there is proposal for closures in java. I have been waiting for this to happen since I haven’t even started programming in java in 1998, when I learned Smalltalk and wrote quite a useful system within two weeks in it.

I like most of the ideas in the proposal – good job gentelman! (no ladies participated yet…)

One thing which I find annoying is the declaration of types for the parameters of the closure, i.e.

Transaction txn, Session s =>

It seams to me that if runWithinTransaction() accepts {Transaction txn, Session s => boolean} closure type, it is kind of redundant to repeat the types of parameters in the closure itself. So I would appreciate a bit of syntactic sugar here (or actually lack of it) and do it as follows:

txn, s =>

Or even like other languages (Smalltalk, Ruby):

runWithinTransaction(){ |txn, s| ... }

I know I might sound like an ignorant here. The compiler writers will have more difficult job with guessing types of the parameters.
Purists might also start crying about lack of explicity here, as you can’t see the parameters type at the first sight… Come on it is no worse than casts when using collections in java under 1.5.

I wish we will see closures in java soon!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: