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 2021/10/10 16:08:00 UTC

[jira] [Assigned] (GROOVY-10270) Cannot initialize parameterized class with lambda without cast

     [ https://issues.apache.org/jira/browse/GROOVY-10270?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles reassigned GROOVY-10270:
------------------------------------

    Assignee: Eric Milles

> Cannot initialize parameterized class with lambda without cast
> --------------------------------------------------------------
>
>                 Key: GROOVY-10270
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10270
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 4.0.0-alpha-3, 4.0.0-beta-1, 3.0.9
>            Reporter: Stefanos Chaliasos
>            Assignee: Eric Milles
>            Priority: Major
>
> I have the following Groovy program.
> {code:groovy}
> import java.util.function.Function;
> @groovy.transform.TypeChecked
> class Main {
>    static final void foo(Function<Short, Byte> x) {}
>    public static final void main() {
>      foo({Short y -> (Byte) y}) // works
>      Cls cls = new Cls({Short y -> (Byte) y}) // works
>      PCls<Function<Short, Byte>> pcls1 = new PCls<Function<Short, Byte>>({Short y -> (Byte) y} as Function<Short, Byte>) // works
>      PCls<Function<Short, Byte>> pcls2 = new PCls<Function<Short, Byte>>({Short y -> (Byte) y}) // fails
>    }
> }
> class Cls {
>   public Function<Short, Byte> f
>   public Cls(Function<Short, Byte> f) {
>      this.f = f
>   }
> }
> class PCls <T> {
>    public T f
>    public PCls(T f) {
>       this.f = f
>    }
> }
> {code}
> h2. Actual Behavior
> The program does not compile, and I get the following error.
> {code:java}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Main.groovy: 14: [Static type checking] - Cannot call PCls#<init>(java.util.function.Function<java.lang.Short, java.lang.Byte>) with arguments [groovy.lang.Closure]
> @ line 14, column 41.
> unction<Short, Byte>> pcls2 = new PCls<F
> ^
> 1 error
> {code}
> h2. Expected Behavior
> Compile successfully.
> h2. Comment
> Tested against 4-beta-1, 4-alpha-3, 3.0.9, and master (commit: b26dbbeabe1e3d27d30c936701b1a571e24d5ab2). Maybe similar to GROOVY-10254, but with parameterized class.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)