[ 
https://issues.apache.org/jira/browse/GROOVY-11397?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17852059#comment-17852059
 ] 

Eric Milles commented on GROOVY-11397:
--------------------------------------

When you write "{ Exec ex -> ... } as Action", you are explicitly excluding the 
type argument from {{Action<T>}}.  I think there was a hole in type checking 
where it would still try to infer the effective type argument or stick with 
what was written even if it did not fit.  But it should enforce the erasure of 
{{T}} as the expected parameter type in this case.

You may also drop "as Action" or "as Action<Exec>" and the type checker can 
figure out that {{Action<Exec>}} is the target type.  You can even drop "Exec 
it ->" and that can be inferred as well.

> Regression in SAM parameter type checking
> -----------------------------------------
>
>                 Key: GROOVY-11397
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11397
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 3.0.18, 3.0.19, 3.0.20, 3.0.21
>            Reporter: Octavia Togami
>            Priority: Minor
>
> A regression in 3.0.18, possibly related to 
> https://issues.apache.org/jira/browse/GROOVY-11013, causes the following code 
> to no longer compile:
> {code:java}
> @groovy.transform.CompileStatic
> class TestClass {
>     interface Action<T> { void run(T t); }
>     interface Exec { void doSomething(); }
>     private Exec configExec(Action<Exec> action) { return null; }
>     private Exec createExec() {
>         return configExec({ Exec it ->
>             it.doSomething()
>         } as Action)
>     }   
> }
> {code}
> {code:java}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
> failed:
> TestClass.groovy: 11: [Static type checking] - Cannot find matching method 
> java.lang.Object#doSomething(). Please check if the declared type is correct 
> and if the method exists.
>  @ line 11, column 13.
>                it.doSomething()
>                ^
> 1 error
> {code}
> It appears that for some reason, the cast to `Action` is overriding the more 
> explicit type of the parameter, resulting in `it` now having the type of 
> `Object`. Changing the cast to be `as Action<Exec>` fixes the problem.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to