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 2021/01/14 21:26:00 UTC

[jira] [Commented] (GROOVY-9882) STC won't coerce closures to generic SAMs

    [ https://issues.apache.org/jira/browse/GROOVY-9882?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17265317#comment-17265317 ] 

Eric Milles commented on GROOVY-9882:
-------------------------------------

Can you provide some more context?  I run the following script with Groovy 3.0.7 and it runs fine:
{code:groovy}
@groovy.transform.CompileStatic
def test() {
    java.util.function.Supplier<String> supplier = { '123' }
    supplier.get()
}
assert test() == '123'
{code}

> STC won't coerce closures to generic SAMs
> -----------------------------------------
>
>                 Key: GROOVY-9882
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9882
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 3.0.7
>            Reporter: Christopher Smith
>            Assignee: Eric Milles
>            Priority: Minor
>
> In static mode, the compiler correctly coerces closures that match non-generic SAMs to their concrete type, but this breaks in the context of generics.
> {code:groovy}
> Runnable doIt = { '123' } // works fine
> Supplier<String> supplier = { '123' } // error
> {code}
> The error is
> {code}
> Cannot assign value of type groovy.lang.Closure <java.lang.String> to variable of type java.util.function.Supplier <String>
> {code}
> Not sure if it's relevant that the generic in the second case is reported as bare {{<String>}}. Replacing the type parameter with a literal {{<java.lang.String>}} causes the error message to match but does not fix the error. Using {{<Function<String, String>}} produces matching behavior, so it's not just a "skip java.lang", and in the latter case the reported bound is the raw type {{Function}}.



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