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/04/28 15:47:00 UTC

[jira] [Commented] (GROOVY-10064) STC infers incorrect type for closure parameter

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

Eric Milles commented on GROOVY-10064:
--------------------------------------

There have been a number of generics fixes made in Groovy 4 that did not get backported to Groovy 3.  Can you give Groovy 4.0a3 a try so we can tell if it is fixed there?

> STC infers incorrect type for closure parameter
> -----------------------------------------------
>
>                 Key: GROOVY-10064
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10064
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 3.0.8
>            Reporter: Christopher Smith
>            Assignee: Eric Milles
>            Priority: Major
>
> I have this subroutine in some code managing DynamoDB tables; the types are from the Amazon 2 SDK and Vavr. The {{Either#fold}} method takes a {{Function<? super L, ? extends U>}} and a {{Function<? super R, ? extends U>}} (collapsing either a "left" or a "right" value into a single result). In this particular case, both types are the same {{DynamoDbTable}}, just representing different process states. The result type is a {{Try<DynamoDbTable>}}.
> Aside from needing the workarounds that I think are related to GROOVY-10033 ({{.&}} and {{as CheckedRunnable}}), this code _works as expected_ in GRECLIPSE, but it fails when compiled with groovyc.
> {code:groovy}
>     private Try<DynamoDbTable> waitForTableActive(Either<DynamoDbTable, DynamoDbTable> maybeActive) {
>         maybeActive.fold(
>             Try.&success, // we already determined it's active
>             table -> {
>                 log.debug('waiting for table {} to become active', table.tableName())
>                 Try.success(table)
>                     .andThenTry({ poll.until { checkActive(table).isLeft() } } as CheckedRunnable)
>             }
>         )
>     }
>     Either<DynamoDbTable, DynamoDbTable> checkActive(DynamoDbTable table) {}
> {code}
> {code}
> Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.10.0:compile (default) on project azimuth-server: Error occurred while calling a method on a Groovy class from classpath.: InvocationTargetException: startup failed:
> DynamoDbSchemaRegistryImpl.groovy: 134: [Static type checking] - Cannot find matching method io.vavr.control.Try#tableName(). Please check if the declared type is correct and if the method exists.
>  @ line 134, column 68.
>    r table {} to become active', table.tabl
>                                  ^
> DynamoDbSchemaRegistryImpl.groovy: 136: [Static type checking] - Cannot find matching method co.vendorflow.azimuth.server.infrastructure.data.dynamodb.DynamoDbSchemaRegistryImpl#checkActive(io.vavr.control.Try <T>). Please check if the declared type is correct and if the method exists.
>  @ line 136, column 59.
>    Try({ poll.until { checkActive(table).is
>                                  ^
> DynamoDbSchemaRegistryImpl.groovy: 136: [Static type checking] - Cannot find matching method java.lang.Object#isLeft(). Please check if the declared type is correct and if the method exists.
>  @ line 136, column 48.
>       .andThenTry({ poll.until { checkActiv
>                                  ^
> DynamoDbSchemaRegistryImpl.groovy: 131: [Static type checking] - Cannot call <U> io.vavr.control.Either <DynamoDbTable, DynamoDbTable>#fold(java.util.function.Function <? super software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable, ? extends U>, java.util.function.Function <? super software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable, ? extends U>) with arguments [groovy.lang.Closure <io.vavr.control.Try>, groovy.lang.Closure] 
>  @ line 131, column 9.
>            maybeActive.fold(
>            ^
> {code}
> I have absolutely no idea how the STC gets the idea that the _parameter_ of the outer closure should be a {{Try}} (the _result_ should be), and it seems possible that the rest of the errors are a cascade from that initial mistake.
> Adding an explicit {{(DynamoDbTable table) ->}} gets groovyc to compile it, but then I still need to say {{as Function}} everywhere or I get complaints that "closure is not a function".



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