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

Types not inferred when coercing closures of generic method return type

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.0
    • 2.3.0-rc-1
    • Static Type Checker
    • None

    Description

      I can't quite narrow down the exact cause of this.

      This fails:

      // Note: this interface must be in different script because of GROOVY-6670
      interface Converter<F, T> {
        T convert(F from)
      }
      
      class Holder<T> {
        T thing
      
        Holder(T thing) {
          this.thing = thing
        }
      
        def <R> Holder<R> convert(Converter<? super T, ? extends R> func1) {
          new Holder(func1.convert(thing))
        }
      }
      
      @CompileStatic
      void m() {
        new Holder<Integer>(2).convert {
          it
        } convert {
          it.floatValue() // fails, doesn't know 'it' is an Integer
        }
      }
      
      m()
      

      However, this works and I can't work out what the difference is.

      interface Action<T> {
        void execute(T thing)
      }
      
      class Thing<T> {
        T thing
        Thing(T thing) {
          this.thing = thing
        }
        
        void doWith(Action<? super T> action) {
          action.execute(thing)
        }
      }
      
      
      class Container {
        @groovy.transform.CompileStatic 
        static m() {
          makeThing("a") doWith { println it.toUpperCase() } // works
          makeThingFromCallable { "a" } doWith { println it.toUpperCase() } // works
        }
        
        static <T> Thing<? extends T> makeThing(T thing) {
          new Thing<T>(thing)
        }
        
        static <T> Thing<? extends T> makeThingFromCallable(java.util.concurrent.Callable<T> thing) {
          new Thing<T>(thing.call())
        }
        
      }
      
      Container.m()
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            ldaley Luke Daley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: