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

CompileStatic: @ClosureParams in @DelegatesTo doesn't work

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0.4
    • 4.0.0-alpha-1, 3.0.5, 2.5.13
    • Static Type Checker
    • None
    • Tested in GroovyConsole 3.0.4

    Description

      This code doesn't compile with this error:

      [Static type checking] - Cannot find matching method java.lang.Object#toBigDecimal(). Please check if the declared type is correct and if the method exists.

      Explicitly adding a `delegate.someFunctionName` works around the problem.

      import groovy.transform.CompileStatic
      import groovy.transform.stc.ClosureParams
      import groovy.transform.stc.FirstParam
      import groovy.transform.stc.FromString
      
      @CompileStatic
      class Delegate {
      
        def <T2> void someFunctionName(Collection<T2> values, @ClosureParams(FirstParam.FirstGenericType) Closure<String> optionKey) {
        }
      
        static Delegate of(@DelegatesTo(Delegate) Closure c) {
          def d = new Delegate()
          c.delegate = d
          c()
          return d
        }
      }
      
      @CompileStatic
      class App {
      
        List<Integer> someList
      
        Delegate datePicker = Delegate.of {
          someFunctionName(
            this.someList,
            // Here: Cannot find matching method java.lang.Object#toBigDecimal(). Please check if the declared type is correct and if the method exists.
            // But it should be known to be Integer
            // The code compiles when delegate.someFunctionName is used above
            { it.toBigDecimal().toString() },
          )
        }
      }
      
      new App()
      

       

      Attachments

        Activity

          People

            emilles Eric Milles
            fesc Felix Scheinost
            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