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

Type not inferred from Iterator<Data<String>>

    XMLWordPrintableJSON

Details

    Description

      Given a Generic DTO

      @groovy.transform.CompileStatic
      @groovy.transform.Immutable( knownImmutableClasses = [T] )
      class Data<T> {
        T value
      }
      

      We can then make an iterator that returns a String variant of these:

      @groovy.transform.CompileStatic
      class StringDataIterator implements Iterator<Data<String>> {
        boolean hasNext() { true }
        void    remove()  {}
      
        Data<String> next() {
          new Data<String>( value: 'tim' )
        }
      }
      

      But when we come to test this all:

      @groovy.transform.CompileStatic
      class Runner {
        static main( args ) {
          Data<String> elem = new StringDataIterator().next()
          println elem.value.length()
        }
      }
      

      We get:

      [Static type checking] - Cannot find matching method java.lang.Object#length(). Please check if the declared type is right and if the method exists.
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            tim_yates Tim Yates
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: