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

Typed closure as a default parameter requires cast when method is TypeChecked

    XMLWordPrintableJSON

Details

    Description

      If you have a typed Closure as a default parameter of a method ie:

      @groovy.transform.TypeChecked
      Integer a( String s, Closure<Integer> b={ String it -> it.length() } ) {
        b( s )
      }
      
      a( 'tim' )
      

      You get the exception:

      [Static type checking] - Cannot return value of type java.lang.Object on method returning type java.lang.Integer -> java.lang.Integer

      The workaround is to either cast the return:

        (Integer)b( s )
      

      Or stop using the default parameter:

      @groovy.transform.TypeChecked
      Integer a( String s ) {
        Closure<Integer> b={ String it -> it.length() }
        b( s )
      }
      
      a( 'tim' )
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            tim_yates Tim Yates
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: