[
https://issues.apache.org/jira/browse/GROOVY-11683?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17958794#comment-17958794
]
Eric Milles commented on GROOVY-11683:
--------------------------------------
Replacing "List::stream" with "list -> list.stream()" allows the type of the
{{flatMap}} stage to be determined to be {{Stream<UserType>}}. Groovy 4 visits
method reference call args before visiting the call expr. Closures and lambdas
are visited after and benefit from some additional inferencing provided by the
method's generics. Groovy 5 visits method pointers and references after --
matching closures and lambdas.
Some more detail in this specific scenario: when visiting "List<Stream>" the
type checker looks for methods of {{Class<List>}} and {{List}} (raw type).
From this it works out "Stream<Object>" as the return type for the {{flatMap}}
stage.
Java lets you add type args to method references, like
"List::<UserType>stream". GROOVY-9239 requests support for this. Groovy
parses "List<UserType>::stream" and this makes its way through STC. I haven't
found exactly where this type info is lost.
You can also add a type hint like this: {{.<UserType>flatMap(List::stream)}}
> STC loses generic information on method reference
> -------------------------------------------------
>
> Key: GROOVY-11683
> URL: https://issues.apache.org/jira/browse/GROOVY-11683
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 4.0.27
> Reporter: Christopher Smith
> Assignee: Eric Milles
> Priority: Minor
>
> I am using a typical stream pipeline to get a list of pages of results and
> flatMap them to the individual records:
> {code}
> //
> software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient
> cognito
> cognito.listUsersPaginator { it.userPoolId(cognitoUserPool) }
> .stream()
> .map(ListUsersResponse::users)
> .flatMap(List::stream) // produces UserType
> .map(UserType::username)
> .toList()
> {code}
> Both groovyc and GRECLIPSE report the compile-time error
> {code}
> Failed to find class method 'username(java.lang.Object)' or instance method
> 'username()' for the type:
> software.amazon.awssdk.services.cognitoidentityprovider.model.UserType
> {code}
> The cause appears to be a failure to propagate the type information out of
> the {{flatMap}} call: On hover, Eclipse correctly identifies the
> {{List::stream}} as returning {{Stream<UserType>}}, but the {{flatMap}} is
> reported as {{Stream<Object>}}. Inserting a type witness before the
> {{flatMap}} call resolves the error. Both the loss of type information and
> the incorrect error message appear to be bugs.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)