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

STC: cannot return closure from method without casting

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      import java.util.function.Supplier;
      
      @groovy.transform.TypeChecked
      public class Main {
          public static Supplier<Integer> foo() {
              { -> 10} as Supplier<Integer> // works
          }
          public static Supplier<Integer> bar() {
              { -> 10} //fails
          }
          public static void main(String[] args) {
              Supplier<Integer> v1 = { -> 10} as Supplier<Integer> // works
              Supplier<Integer> v2 = { -> 10} // works
              Supplier<Integer> v3 = foo()
              Supplier<Integer> v4 = bar()
          }
      }
      

      Actual Behavior

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

      Main.groovy: 10: [Static type checking] - Cannot return value of type groovy.lang.Closure<java.lang.Integer> on method returning type java.util.function.Supplier<java.lang.Integer>
       @ line 10, column 9.
                 { -> 10} //fails
                 ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: