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

Types not inferred when coercing closures of generic method return type

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.0
    • Fix Version/s: 2.3.0-rc-1
    • Component/s: Static Type Checker
    • Labels:
      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

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

              Dates

              • Created:
                Updated:
                Resolved: