Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10047

STC infers Closure instead of SAM for method references

    XMLWordPrintableJSON

Details

    Description

      I thought I had reported this earlier, but I couldn't find the duplicate.

      When using a very basic method reference, the STC is inferring Closure as the type instead of the proper functional interface. Explicitly specify as works, which I think might not have with the :: syntax previously.

      In my particular case, I'm wrapping a List<DeleteItemRequest> (from the Amazon SDK 2 DynamoDB client) into a Map<String, List<DeleteItemRequest>> to feed to its batch API. The line is:

      def requestItems = batch.stream().collect(toMap(DeleteItemRequest::tableName, List::of))
      

      In contrast to GROOVY-9881, it seems the STC is correctly identifying the signature, at least, but it's inferring Closure where Function is expected:

      /home/christopher/path/CleanupHandler.groovy: 50: [Static type checking] - Cannot call <T,K,U> java.util.stream.Collectors#toMap(java.util.function.Function <? super T, ? extends K>, java.util.function.Function <? super T, ? extends U>) with arguments [groovy.lang.Closure <java.lang.String>, groovy.lang.Closure <java.util.List>]
      

      Adding an explicit as Function to each reference fixes the problem (though it is still using getMethodPointer instead of real method references).

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              chrylis Christopher Smith
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: