Groovy 编译器不接受 java 8 Lambdas [英] Groovy compiler does not accept java 8 Lambdas

查看:28
本文介绍了Groovy 编译器不接受 java 8 Lambdas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,Groovy 语法接受闭包.今天,Java 8 也增加了它的语法闭包.

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

但是,当我在 groovy 文件中编写 java8 闭包时,出现如下错误:

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

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

我们收到此错误:

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()  

推荐答案

是的,Groovy 解析器不接受 Java 8 Lambda(不是闭包).

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

您可以使用闭包代替它(假设您使用的是 Groovy 2.3.*)

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

即:

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

Groovy 3.0+ 将接受 lambda 格式

edit:

Groovy 3.0+ will accept lambda format

这篇关于Groovy 编译器不接受 java 8 Lambdas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆