You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Christopher Smith (Jira)" <ji...@apache.org> on 2020/10/28 20:51:00 UTC

[jira] [Created] (GROOVY-9796) Static type checker doesn't backtrack when solving generics

Christopher Smith created GROOVY-9796:
-----------------------------------------

             Summary: Static type checker doesn't backtrack when solving generics
                 Key: GROOVY-9796
                 URL: https://issues.apache.org/jira/browse/GROOVY-9796
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
    Affects Versions: 3.0.6
            Reporter: Christopher Smith


When a method pipeline involves generics, the static type checker is greedy and tends to be overly specific, producing errors when the correct answer involves relaxing to a common bound.


{code:groovy}
@CompileStatic
Number returnANumber() {
  Number number = Optional.of(1234 as Integer) // should infer Optional<Number>
    orElse(42.0 as Double)
}
{code}

{code}
[Static type checking] - Cannot find matching method java.util.Optional#orElse(java.lang.Double). Please check if the declared type is correct and if the method exists.
{code}
Explicitly inserting a type witness `Optional.<Number>of` works.

Note: This _might_ be a duplicate of GROOVY-8103, but I'm unable to tell precisely. 3.0.6 was released one week after it was marked resolved. GROOVY-8961 is marked fixed in 3.0.6, but I'm having the problem there.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)