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):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerNotifier {
//input parameters of closure are implicit
void notifyWithClosure(CustomerId customerId, Closure<Email> createsEmail) {
def customer = this.customerRepository.getBy(customerId)
def email = createsEmail.call(customer, environment)
this.emailSender.sendEmail(email)
}
}
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerNotifier {
//input parameters of closure are defined as closureParams
void notifyWithClosureParams(CustomerId customerId,
@ClosureParams(value = SimpleType.class, options = ["Customer", "String"]) Closure<Email> createsEmail) {
def customer = this.customerRepository.getBy(customerId)
def email = createsEmail.call(customer, environment)
this.emailSender.sendEmail(email)
}
}
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerNotifier {
//input parameters of function are explicit
void notifyWithFunction(CustomerId customerId, F2<Customer, String, Email> createsEmail) {
def customer = this.customerRepository.getBy(customerId)
def email = createsEmail.f(customer, environment)
this.emailSender.sendEmail(email)
}
}
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customerNotifier.notifyWithFunction(customerId) { customer, env ->
new Email(env == 'dev' ? 'phonyEmail@me.com' : customer.emailAddress, "noreply@me.com", 'subject', 'content')
}
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.
You can find a gist of the complete example here.
Greetings
Jan
No comments:
Post a Comment