Christopher Smith created GROOVY-11051:
------------------------------------------

             Summary: 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


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)

Reply via email to