Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

closures - Groovy compiler does not accept java 8 Lambdas

As , we know , Groovy syntax accepts closures . Today also, Java 8 adds in its syntax closure .

However , When i write java8 closure in groovy file , i get an error like the following :

Person.findAll().stream().filter(e-> e.age > 20)

We get this error :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 7: unexpected token: -> @ line 7, column 39.
   Person.findAll().stream().filter(e-> e.controllerId > 0)
                                         ^

1 error

Nevertheless , the following works successully :

Person.findAll().stream()  
question from:https://stackoverflow.com/questions/23906748/groovy-compiler-does-not-accept-java-8-lambdas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yeah, the Groovy parser does not accept Java 8 Lambdas (not closures).

You can use a closure in place of it (assuming you're on Groovy 2.3.*)

ie:

Person.findAll().stream().filter( { e -> e.age > 20 } )

edit:

Groovy 3.0+ will accept lambda format


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...