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

Unable to compile: Expected type java.lang.Object for closure parameter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 3.0.18, 4.0.13
    • None
    • None
    • Java : corretto-11.0.19
      Ubuntu 22.04

    Description

      The following code fails to compile:

      @groovy.transform.CompileStatic
      Map<Integer, String> files = new File("/").listFiles().toList().withIndex().collectEntries {
          File file, int index ->
              [(index) : file.name]
      }   
      files.each {
          int i, String name ->
              println String.format("%d : %s", i, name)
      }
      

      With the error:

      > Task :compileGroovy
      startup failed:
      
      groovy-bugs/app/src/main/groovy/com/intellisrc/groovy/App.groovy: 9: [Static type checking] - Expected type java.lang.Object for closure parameter: file
       @ line 9, column 4.
                     File file, int index ->
            ^
      

      I have prepared a small project for testing:

      https://gitlab.com/intellisrc/groovy-bugs/-/tree/with-index?ref_type=heads

      Versions 3.0.17 , 4.0.12 and below seems to work as expected.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              lepe A. Lepe
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: