Groovy equivalent of Java 8 lambda expression
I have only one method that has this java interface@ H_ 419_ 2@// Java Interface public interface AuditorAware { Auditor getCurrentAuditor(); }
I use Java 8 lambda expression to create auditoraware. The content is as follows
I tried to write a java implementation in groovy
I see that there are many ways to implement interfaces in groovy, as shown in this groovy ways to implement interfaces document
I have implemented groovy equivalence on Java code by using the implementation interface with map, as shown in the above-mentioned document
However, as the documentation example shows, it seems more concise to implement the interface using the closure method However, when I try to implement it as follows, IntelliJ displays error codes and blurs code blocks
How can I change the Java 8 lambda implementation to groovy equivalent by using the "implement interface with closures" method?
Solution
As Dylan bijnagte stated, the following code is valid@ H_ 419_ 2@// Groovy Equivalent by "implement interfaces with a closure" method AuditorAware currentAuditor() { { -> AuditorContextHolder.auditor} as AuditorAware }
The chapter parameter documentation on groovy closure explains this