You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/01/30 03:33:00 UTC

[jira] [Commented] (GROOVY-6745) Java8 Lambda var args and Closures

    [ https://issues.apache.org/jira/browse/GROOVY-6745?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16755640#comment-16755640 ] 

Paul King commented on GROOVY-6745:
-----------------------------------

Just listing the other supported styles/workarounds:
{code}
println m.isA([{'human'}, {'animal'}] as Supplier[])
println m.isA({'human'} as Supplier, {'animal'} as Supplier)
{code}

> Java8 Lambda var args and Closures
> ----------------------------------
>
>                 Key: GROOVY-6745
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6745
>             Project: Groovy
>          Issue Type: Improvement
>    Affects Versions: 2.3.0-beta-2
>            Reporter: stephen mallette
>            Priority: Major
>
> Text below is taken from this thread in the mailing list:
> http://groovy.markmail.org/thread/xgw5g4fdolxssjj6
> Please see this simple Java8 class:
> {code:java}
> import java.util.function.*;
> class Marko {
>   public isA(Supplier<String> supplier) {
>     return supplier.get();   
>   }
>   public isA(Supplier<String>... suppliers) {
>     String temp = "";
>     for(int i=0; i<suppliers.length; i++) {
>        temp = temp + suppliers[i].get();
>     }   
>     return temp;
>   }
> }
> {code}
> I am delighted that Groovy 2.3.0-beta-2 supports the following auto-conversion from closure to Java8 lambda.
> {code:none}
> gremlin> m.isA{'human'}
> ==>human
> {code}
> However, note the issue with the Supplier[] var args method call.
> {code:none}
> gremlin> m.isA{'human'}{'animal'}
> No signature of method: Marko.isA() is applicable for argument types: (groovysh_evaluate$_run_closure1, groovysh_evaluate$_run_closure2) values: [groovysh_evaluate$_run_closure1@39d9314d, groovysh_evaluate$_run_closure2@b978d10]
> Possible solutions: isA([Ljava.util.function.Supplier;), isA(java.util.function.Supplier), is(java.lang.Object), find(), any(), use([Ljava.lang.Object;)
> Display stack trace? [yN]
> gremlin> m.isA({'human'}{'animal'})
> No signature of method: Marko.isA() is applicable for argument types: (java.lang.String) values: [human]
> Possible solutions: isA([Ljava.util.function.Supplier;), isA(java.util.function.Supplier), is(java.lang.Object), use([Ljava.lang.Object;), find(), any()
> Display stack trace? [yN]
> gremlin>
> {code}
> It is interesting to note that this conversion works:
> {code:none}
> gremlin> suppliers = [{'human'},{'animal'}] as Supplier[]
> ==>groovysh_evaluate$_run_closure1@727eb8cb
> ==>groovysh_evaluate$_run_closure2@39d9314d
> gremlin> m.isA(suppliers)
> ==>humananimal
> {code}
> It would be sweet if 2.3.0 GA provided support for auto conversion of closure var args to Java8 function var args.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)