You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Stefanos Chaliasos (Jira)" <ji...@apache.org> on 2021/09/23 03:31:00 UTC

[jira] [Created] (GROOVY-10254) STC: cannot return closure from method without casting

Stefanos Chaliasos created GROOVY-10254:
-------------------------------------------

             Summary: STC: cannot return closure from method without casting
                 Key: GROOVY-10254
                 URL: https://issues.apache.org/jira/browse/GROOVY-10254
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation, Static Type Checker
    Affects Versions: 4.0.0-beta-1
            Reporter: Stefanos Chaliasos


I have the following Groovy program.
{code:groovy}
import java.util.function.Supplier;

@groovy.transform.TypeChecked
public class Main {
    public static Supplier<Integer> foo() {
        { -> 10} as Supplier<Integer> // works
    }
    public static Supplier<Integer> bar() {
        { -> 10} //fails
    }
    public static void main(String[] args) {
        Supplier<Integer> v1 = { -> 10} as Supplier<Integer> // works
        Supplier<Integer> v2 = { -> 10} // works
        Supplier<Integer> v3 = foo()
        Supplier<Integer> v4 = bar()
    }
}
{code}
h2. Actual Behavior

The program does not compile, and I get the following error.
{code:java}
Main.groovy: 10: [Static type checking] - Cannot return value of type groovy.lang.Closure<java.lang.Integer> on method returning type java.util.function.Supplier<java.lang.Integer>
 @ line 10, column 9.
           { -> 10} //fails
           ^

1 error

{code}
h2. Expected Behavior

Compile successfully.



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