[
https://issues.apache.org/jira/browse/GROOVY-12606?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149044#comment-18149044
]
Daniel Sun commented on GROOVY-12606:
-------------------------------------
Fixed on master via #3133 (https://github.com/apache/groovy/pull/3133,
5a45f2e0e321fa988483a3557902f5cb70a7578c,
https://github.com/apache/groovy/commit/5a45f2e0e321fa988483a3557902f5cb70a7578c)
and on the GROOVY_6_0_X branch in 76e69a5cbd4c4fca2b9dee8a95c0f3cd37255b27
(https://github.com/apache/groovy/commit/76e69a5cbd4c4fca2b9dee8a95c0f3cd37255b27).
> STC rejects a closure or lambda returning a Closure<R> value where a
> functional interface return type is expected
> -----------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12606
> URL: https://issues.apache.org/jira/browse/GROOVY-12606
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Assignee: Daniel Sun
> Priority: Major
> Fix For: 7.0.0-alpha-1, 6.0.1
>
>
> {code:groovy}
> import java.util.function.Supplier
> @groovy.transform.CompileStatic // or @TypeChecked
> class C {
> static String take(Supplier<Supplier<String>> s) { s.get().get() }
> static void main(args) {
> Closure<String> c = { 'x' }
> assert take { c } == 'x'
> assert take(() -> c) == 'x'
> }
> }
> {code}
> Actual:
> {noformat}
> [Static type checking] - Cannot return value of type
> groovy.lang.Closure<java.lang.String> for closure expecting
> java.util.function.Supplier<java.lang.String>
> [Static type checking] - Cannot return value of type
> groovy.lang.Closure<java.lang.String> for lambda expecting
> java.util.function.Supplier<java.lang.String>
> {noformat}
> Expected: both calls compile and the returned closure is converted to the
> functional interface, as it is when a Closure value is passed as a Supplier
> argument. A Closure<Integer> should still be rejected.
> The return check for closures and lambdas only accepts subtypes of the
> expected return type and does not consider closure-to-SAM coercion. A
> returned closure literal and a raw Closure are rejected the same way. Under
> static compilation, a lambda also needs its generated method to declare the
> SAM type, so that the returned closure is converted instead of being returned
> as a Closure.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3133
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)