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

Problem With @CompileStatic And Typed Collections

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.9, 2.3.0, 2.4.0-rc-1
    • 4.0.0-alpha-3
    • Static Type Checker
    • None

    Description

      The following code compiles:

      Demo.groovy
      @groovy.transform.CompileStatic
      class Demo {
          def test(Listener listener1) {
              def listener2 = new DefaultListener()
              List<Listener> listeners = [listener1, listener2]
          }
      }
      
      
      interface Listener {}
      
      class DefaultListener implements Listener {}
      

      This also compiles:

      Demo.groovy
      @groovy.transform.CompileStatic
      class Demo {
          def test(Listener listener1) {
              def listener2 = new DefaultListener()
              List<Listener> listeners = [listener1]
          }
      }
      
      
      interface Listener {}
      
      class DefaultListener implements Listener {}
      

      This will not compile:

      Demo.groovy
      @groovy.transform.CompileStatic
      class Demo {
          def test(Listener listener1) {
              def listener2 = new DefaultListener()
              List<Listener> listeners = [listener2]
          }
      }
      
      
      interface Listener {}
      
      class DefaultListener implements Listener {}
      
      staticcompiledcollection $ groovy -version
      Groovy Version: 2.1.9 JVM: 1.6.0_51 Vendor: Apple Inc. OS: Mac OS X
      staticcompiledcollection $ 
      staticcompiledcollection $ cat Demo.groovy 
      @groovy.transform.CompileStatic
      class Demo {
          def test(Listener listener1) {
              def listener2 = new DefaultListener()
              List<Listener> listeners = [listener2]
          }
      }
      
      
      interface Listener {}
      
      class DefaultListener implements Listener {}
      staticcompiledcollection $ 
      staticcompiledcollection $ groovyc Demo.groovy 
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Demo.groovy: 5: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.List <DefaultListener> to: java.util.List <Listener>
       @ line 5, column 36.
                 List<Listener> listeners = [listener2]
                                            ^
      
      1 error
      
      staticcompiledcollection $
      

      Attachments

        Activity

          People

            emilles Eric Milles
            brownj Jeff Brown
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: