[
https://issues.apache.org/jira/browse/GROOVY-11051?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17727231#comment-17727231
]
Eric Milles commented on GROOVY-11051:
--------------------------------------
This is the STC closure return type check.
https://github.com/apache/groovy/blob/7ccc3057cd675651710fad8721af3a46a9e8bdce/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java#L2256
There are a few limited cases allowed like simple covariance and GString to
String. {{boolean}} is explicitly skipped.
> Static compiler does not coerce Groovy truth from closures
> ----------------------------------------------------------
>
> Key: GROOVY-11051
> URL: https://issues.apache.org/jira/browse/GROOVY-11051
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 4.0.12
> Reporter: Christopher Smith
> Priority: Major
>
> The static compiler will accept a closure returning a non-boolean value as
> fulfilling a {{Predicate}}, but it does not coerce the return value using
> Groovy truth.
> {code}
> @CompileStatic
> class Bug {
> static void main(String[] args) {
> println new AtomicReference<Object>(null).stream()
> .filter( { it.get() } as Predicate<AtomicReference<?>>)
> // or .filter(AtomicReference::get as Predicate<AtomicReference<?>>)
> .findAny()
> }
> }
> {code}
> Expected result: {{Optional.empty}}
> Actual result: NullPointerException on unboxing the {{null}} return value
> Using an explicit {{as boolean}} inside the lambda works, but this doesn't
> work with method references. Coercion should work even without {{as
> Predicate}} if the compiler sees the functional return type is
> boolean/Boolean.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)