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

Unexpected behavior of CompileStatic when assigning a Closure to a new block code

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.1
    • 2.0.4
    • Static compilation
    • None
    • Windows 7

    Description

      @groovy.transform.CompileStatic
      class Foo{
          def run(){
              Closure a ={
                  int i ->
                  println "First closure "+ i
              }
              Closure b ={
                  String s ->
                  println "Second closure "+ s
              }
              a=b
              a("Testing!")
          }
      }
      Foo f = new Foo()
      f.run()
      

      With CS applied the code will fail to compile with the following error:

      [Static type checking] - Closure argument types: [int] do not match with parameter types: [java.lang.String]
      at line: 13, column: 11
      

      The compiler is expecting an integer but since the variable 'a' is re-assigned to a new piece of code that takes a String as a parameter this code should work

      Attachments

        1. closure_bug.groovy
          0.3 kB
          Carlos Cortinhas

        Activity

          People

            melix Cédric Champeau
            carlosapc Carlos Cortinhas
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: