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

Make for-in null safe for CompileStatic

    XMLWordPrintableJSON

Details

    Description

      See following simple script:

      import groovy.transform.CompileStatic;
      
      @CompileStatic
      void printList(List l){
          println "List:"
          for (Object o in l) {println o}
          println "Done"
      }
      
      printList(null)
      

      It crashes with null pointer exception due to list being null in the for loop. However it works perfectly fine when CompileStatic annotation is removed. I believe this inconsistency is a bug as the whole point of introducing a new for-in construct in groovy appeared to be to make it a null safe construct (unlike for (Object o: l) construct in java). However, when compiling statically, it seems to be compiled as the java iterator it replaces, breaking away from groovy convention without even a warning.

      Any chance to have it fixed in 3.x?

       

      Attachments

        Activity

          People

            emilles Eric Milles
            seva_fwd Sergiy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: