You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/07/02 21:56:00 UTC
[jira] [Commented] (GROOVY-9597) CompileStatic: @ClosureParams in
@DelegatesTo doesn't work
[ https://issues.apache.org/jira/browse/GROOVY-9597?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17150603#comment-17150603 ]
Eric Milles commented on GROOVY-9597:
-------------------------------------
Here is a smaller example:
{code:groovy}
import groovy.transform.*
import groovy.transform.stc.*
@CompileStatic
class Help9597 {
def <T> void proc(Collection<T> values, @ClosureParams(FirstParam.FirstGenericType) Closure<String> block) {
}
}
@CompileStatic
class Main9597 {
List<Integer> list
void test(Help9597 h) {
h.proc(this.list) { it.toBigDecimal().toString() } // works
h.with {
proc(this.list) { it.toBigDecimal().toString() } // error
}
}
}
{code}
> CompileStatic: @ClosureParams in @DelegatesTo doesn't work
> ----------------------------------------------------------
>
> Key: GROOVY-9597
> URL: https://issues.apache.org/jira/browse/GROOVY-9597
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 3.0.4
> Environment: Tested in GroovyConsole 3.0.4
> Reporter: Felix Scheinost
> Assignee: Eric Milles
> Priority: Minor
>
> This code doesn't compile with this error:
> {noformat}
> [Static type checking] - Cannot find matching method java.lang.Object#toBigDecimal(). Please check if the declared type is correct and if the method exists.{noformat}
> Explicitly adding a `delegate.someFunctionName` works around the problem.
> {code:groovy}
> 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()
> {code}
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)