Looks like a bug to me, I can reproduce in 2.4.7. The good news is that if you 
have a method taking the Function, like:

static void calc(Function<Integer, Integer> fct) { println fct.apply(10) }

Then you can call it with calc { -it }

You can use the trick to define an identity function to trick the static 
compiler:

static Function<Integer, Integer> f(Function<Integer, Integer> fct) { fct }
Function<Integer, Integer> fct = f { -it }

However, I could not get the following signature to work:

static <A, R> Function<A, R> f(Function<A, R> fct) { fct }

If you don't need full type checking, the following does work:
Function fct = { Integer n ->
            return 1
        }

Jason

-----Original Message-----
From: cazacugmihai [mailto:cazacugmi...@gmail.com] 
Sent: Friday, September 02, 2016 9:06 AM
To: us...@groovy.incubator.apache.org
Subject: Static type checking

Hi, 

I have a problem running this code:
 
import groovy.transform.CompileStatic
import java.util.function.Function
 
@CompileStatic
class Test {
        static void main(String[] args) {
               // this code fails 
                Function<Integer, Integer> fct = { Integer n ->
                        -n
                }
 
                // this one works but it is too verbose
                // Function<Integer, Integer> fct = ({ Integer n ->
                //      -n
                // } as Function<Integer, Integer>)
 
                println fct.apply(10)
        }
}

The error:

Test.groovy: 9: [Static type checking] - Incompatible generic argument types. 
Cannot assign java.util.function.Function <java.lang.Integer, 
groovy.lang.Closure> to: java.util.function.Function <Integer, Integer>  @ line 
9, column 36.
                Function<Integer, Integer> fct = { Integer n ->
                                      ^
1 error

[Finished in 0.5s]

Is there a bug in groovy related to @CompileStatic or maybe I am missing 
something else? I just don't want to write redundant code.

Thanks,
Mihai



--
View this message in context: 
http://groovy.329449.n5.nabble.com/Static-type-checking-tp5735162.html
Sent from the Groovy Users mailing list archive at Nabble.com.

This email message and any attachments are for the sole use of the intended 
recipient(s). Any unauthorized review, use, disclosure or distribution is 
prohibited. If you are not the intended recipient, please contact the sender by 
reply email and destroy all copies of the original message and any attachments.

Reply via email to