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

Closure argument types not inferred when calling a constructor

    XMLWordPrintableJSON

Details

    Description

      Consider this Java class:

      package test64;
      
      import java.util.List;
      import java.util.function.BiConsumer;
      
      public class Test64J<Integer> {
      
      	public Test64J(String foo, BiConsumer<String, ? super List<Integer>> bar) {
      	}
      	
      	public void doSomething(String foo, BiConsumer<String, ? super List<Integer>> bar) {
      	}
      }
      

      and this Groovy class:

      package test64
      
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Test64 {
      
      	void foobar() {
      		Test64J j = new Test64J('hello', { foo, bar ->
      			println(bar.size())
      		})
      		
      		j.doSomething('hello') { foo, bar ->
      			println(bar.size())
      		}
      	}
      }
      

      The static type checker fails to infer types for foo and bar closure arguments in the constructor call (hence compilation fails on the first println call, because it does not know bar is a List), while it succeeds to do the same thing in the method call.

      Attachments

        Activity

          People

            emilles Eric Milles
            mauromol Mauro Molinari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h