You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/05/30 17:49:00 UTC
[jira] [Updated] (GROOVY-11051) Static compiler does not coerce Groovy truth from closures
[ https://issues.apache.org/jira/browse/GROOVY-11051?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles updated GROOVY-11051:
---------------------------------
Fix Version/s: 5.0.0-alpha-1
> 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
> Assignee: Eric Milles
> Priority: Major
> Fix For: 5.0.0-alpha-1
>
>
> 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)