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

Wrong inference when using ternary operator with closure and variable

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      import java.util.function.Supplier;
      
      @groovy.transform.TypeChecked
      class Main {
        static final void foo(Supplier<Integer> x) {}
      
        public static final void main() {
            Supplier<Integer> y = { -> 5 }
            Supplier<Integer> z = (true) ? y : {-> 5}
        }
      }
      
      

      Actual Behavior

      The program does not compile, and I get the following error.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Main.groovy: 11: [Static type checking] - Cannot assign value of type java.lang.Object to variable of type java.util.function.Supplier<java.lang.Integer>
       @ line 11, column 29.
               Supplier<Integer> z = (true) ? y : {-> 5}
                                     ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Comment

      Tested against 4-beta-1, 4-alpha-3, 3.0.9, and master (commit: b26dbbeabe1e3d27d30c936701b1a571e24d5ab2). Maybe similar to GROOVY-10114, but with function types.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              schaliasos Stefanos Chaliasos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: