This post originated from an RSS feed registered with Java Buzz
by Mathias Bogaert.
Original Post: What I miss in Java 8 lambda functions
Feed Title: Scuttlebutt
Feed URL: http://feeds.feedburner.com/AtlassianDeveloperBlog
Feed Description: tech gossip by mathias
I’m a Clover developer at Atlassian, and I had an opportunity to work closely with new language features introduced in Java 8 during development of Clover 3.2.0 (which has the support for Java 8). I’d like to share my impressions about a major language feature – lambda functions. I’m pretty sure you’ve already read a lot of articles about lambdas, and already know how they are going to reduce boilerplate code. And I totally agree with this point. I can bet that most of anonymous in-line classes you have in your code might be changed to a simple lambda function, quite often written as a one-liner. But are lambda functions perfectly designed? In my opinion – no. Let me show why. Writing a lambda definition Let’s start with writing a simple action listener: JButton button = new JButton("Click me!"); button.addActionListener(event -> button.setText("Thank you!")); This looks great. Short and concise as expected – A huge win for Java 8. Writing a lambda declaration So let’s start writing our own lambda caller! It will take a single BigInteger as input, make some computation on it, and return another BigInteger: public class Main { public static void runCalc(WHAT_SHOULD_I_WRITE calc) { // ??? } public static void main(String[] args) { runCalc(a -> a.multiply(a)); } } But how to declare a lambda signature in WHAT_SHOULD_I_WRITE? In Scala we could write: def runCalc(calc: (BigInt => BigInt)) { ??? } So, let’s try to do something similar in Java: public static void runCalc((BigInteger -> BigInteger) calc) { // ??? } Unfortunately, this code does not compile: java: illegal start of type Do you think that maybe a syntax is different for such declaration? No. So what is wrong here? Actually nothing. It’s just impossible. Really. There is no possibility to declare a signature of an anonymous function in Java 8. What you have to do is to declare a functional interface, i.e. an interface having exactly one abstract method. And a signature of this method defines types of input arguments and a return type of a lambda function we can assign to it. In our case this is a single BigInteger argument and a BigInteger return value. So let’s write it: public class Main { interface MyCalcLambda { BigInteger run(BigInteger input); } public static void runCalc(MyCalcLambda calc) { // ??? } public static void main(String[] args) { runCalc(a -> a.multiply(a)); } } In my opinion this is weird – why should I declare such interface at all? Why isn’t it possible to declare input types and a return type just in-line? Lambda functions have been introduced in Java 8 in order to get rid of anonymous in-line classes, right? So why, instead of this, do we have to declare functional interfaces? There are two things which may cheer you up, however: Writing lambda definition is more frequent than its declaration, so in 90 percent of cases you won’t have to write functional interfaces. The java.function package contains few dozens of predefined functional interfaces, so in most cases […]