Groovy compiler does not accept java 8 Lambdas
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
Abdennour TOUMI
Instructor of Course Run Kubernetes on AWS with EKS. Certified: 🥇 CKA - Kuberntes administrator k8s 🥇 CKAD - Kuberntes App Dev k8s 🥇 AWS DevOps Engineer - Professional devops aws 🥇 Professional Cloud Architect - Google Cloud google-cloud-platform 🥇 AWS Solutions Architect - Professional architecture aws 🥇 Red HAT Certified in Ansible Automation ansible devops 🥇 Red HAT Certified in Openshift Administration ocp 🥇 Red HAT Certified in Openshift App Development ocp 🥇 Red HAT Certified Engineer redhat 🏅 AWS Solutions Architect - Associate architecture 🏅 AWS SysOps Administrator - Associate aws 🏅 AWS Developer - Associate aws 🏅 Linux Professional Institute (LPIC-1) linux 🏅 Professional ReactJS Developer (Udacity Nanadegreee) react frontend 🏅 Fullstack web Developer (Udacity Nanadegreee) python flaskrest Top 1 Stackoverflow reputation in my country Tunisia since 2017 Read more about me: in.abdennoor.com
Updated on July 09, 2022Comments
-
Abdennour TOUMI almost 2 years
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()
-
Will almost 10 yearsGroovy is scheduled to accept Java's 8 syntax on version 3
-
Abdennour TOUMI almost 10 yearsReally , java 8 will let all programmer to not return to groovy
-
tim_yates almost 10 years@AbdennourToumi Apart for the ease of scripting, meta-programming, dynamic types, AST transformations, data parsing, ease of DSL creation, and being able to Grab resources, you may be right ;-)
-
Daniil Shevelev about 9 yearsI tried following code in Groovy and "x" is the array and not an element. "Arrays.stream(array).filter({ x -> x.equals(dependent) || x.equals(governor) }).toArray();" Any suggestions?
-
tim_yates about 9 years@DaSh my guess is that what you have named
array
is a list -
medge over 8 years@tim_yates using @CompileStatic in this case generates compilation errors "filter in java.util.Stream cannot be applied to groovy.lang.Closure". IntelliJ's proposed fix was something on the order of
lst.stream().filter({ from, to -> to.equals(node) } as Predicate<? super Tuple>)
. This is as of Groovy 2.4.5. Thoughts? (For reference, the lst var was a Set<Tuple>) -
Gangnus over 6 years@tim_yates Please, add a variant for two arguments lambdas, for in Java it needs braces, and in Groovy we need to omit them. And thank you, it helped anyway.
-
James almost 3 years@AbdennourTOUMI it's 2021 w/ Java 16 fairly well established in the wild and I still very much prefer Groovy over Java ¯_(ツ)_/¯