Thursday, February 5, 2015

... about type safe closures in groovy

I've been using groovy for a couple of years now. I love it. It saved me from several nervous breakdowns coding in the pre-jdk8 world.

However, there's one thing that starts to annoy me.


It's closures. 
At least when passed as a parameter in a method, mimicking higher order functions.

In the following example, there's a CustomerNotifier that sends an email to a customer. It accepts a CustomerId and a closure creating the email. The closure is called with the Customer and a string indicating the environment ( we don't want to send emails to the real customers in a dev environment, do we):

Loading code....

In the method signature, there's no way to express what input parameters the closure needs. I can express the result, but not the input parameters.

That's annoying because now, the caller of that method, must look into the implementation to know what arguments will be passed to the closure. To make things worse, your IDE will not be able to assist you because it too won't have a clue of the parameters it expects. It's like typing blindfolded.

I could use @ClosureParams. This annotation provides extra information about the parameters a closure needs, assisting the IDE. However it just makes your code really ugly:

Loading code....

Lately I started using a library called functional java. It facilitates programming functionally in java and can be used by all poor souls that aren't allowed to program in scala but do want to use a more functional approach.

Among the basic stuff it contains interfaces for functions with 1 up till 8 input parameters. With these I can clearly define what input parameters my function needs.


Loading code....

Together with groovy's implicit closure coercion, I can use closures as typed functions while my IDE will resolve the input parameters for me and warn me when using the wrong type.

Loading code....

I'll definitely be using the functionalJava library more as it works great with groovy. There's also a groovy library building on top of functionalJava at https://github.com/mperry/functionalgroovy. It makes working with groovy even smoother but up till now I haven't really felt the need to use it.

You can find a gist of the complete example here.

Greetings
Jan