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

Saravanan commented on GROOVY-11615:
------------------------------------

You are right, it works in 5.x, but this does not work (I am not able to figure 
out a pattern here, sorry)

 
{code:java}

import com.google.common.collect.ImmutableMap;
import java.util.stream.Collectors;
public class Interfaces {
    public String myThing() {
        var myArray = ImmutableMap.of("123", "1354");
        // This call will fail.
        //    Incompatible generic argument types. Cannot assign 
java.util.Map<java.lang.Integer, java.lang.Object>
        //    to: java.util.Map<java.lang.Integer, java.lang.String>
        // To make it work change it to
        //    Map<Integer, String> mySet = 
myArray.entrySet().stream().map(entry ->
        //        String.format(entry.getKey() + "-" + entry.getValue())
        //    ).collect(Collectors.<String, Integer, 
String>toMap(String::length, value -> value));
        Map<Integer, String> mySet = myArray.entrySet().stream().map(entry ->
            String.format(entry.getKey() + "-" + entry.getValue())
        ).collect(Collectors.<String, Integer, String>toMap(String::length, 
value -> value));
    }
}
{code}
 

> Groovy has trouble type inferencing for Collections. The offender is 
> Collectors.asBlah set of methods. It needs the generic type <A.Y.X> to be 
> added explicitly
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-11615
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11615
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 4.0.24
>            Reporter: Saravanan
>            Priority: Minor
>
> {code:java}
> public class Interfaces {
>     public String myThing() {
>         var myArray = Arrays.asList("123", "1354");
>         var mySet = myArray.stream().collect(Collectors.toSet());
>         
>         // This call will fail
>         //      Failed to find class method 'getBytes(java.lang.Object)' or
>         //      instance method 'getBytes()' for the type: java.lang.String
>         // To make it work change the previous line to
>         //      var mySet = 
> myArray.stream().collect(Collectors.<String>toSet());
>         mySet.forEach(String::getBytes);
>     }
> }{code}
> Most Collectors.<methods> need a fully qualified type for the generic 
> parameter, without which the compile will fail (sometimes detect an Object 
> type instead of the right one)



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to