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 2022/05/19 17:50:00 UTC

[jira] [Resolved] (GROOVY-10272) Wrong inference when using ternary operator with lambdas

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

Eric Milles resolved GROOVY-10272.
----------------------------------
    Fix Version/s: 5.0.0-alpha-1
       Resolution: Fixed

https://github.com/apache/groovy/commit/16f6be8639c15e227efc0154604b6bd8f8ffe1ae

> Wrong inference when using ternary operator with lambdas
> --------------------------------------------------------
>
>                 Key: GROOVY-10272
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10272
>             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
>             Fix For: 5.0.0-alpha-1
>
>
> I have the following Groovy program.
> {code:groovy}
> import java.util.function.Function;
> @groovy.transform.TypeChecked
> class Main {
>     public static final void main() {
>         Function<Long, Double> x
>         // Works
>         if (true) {
>             x = {Long a -> (Double) a}
>         } else {
>             x = {Long b -> (Double) b}
>         }
>         // Fails
>         Function<Long, Double> y = ((true) ? {Long a -> (Double) a} : {Long b -> (Double) b})
>     }
> }
> {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: 16: [Static type checking] - Cannot assign value of type java.lang.Double to variable of type java.util.function.Function<java.lang.Long, java.lang.Double>
>  @ line 16, column 36.
>            Function<Long, Double> y = ((true) ? {Long a -> (Double) a} : {Long b -> (Double) b})
>                                       ^
> 1 error
> {code}
> Note that the computed type is the return type of the lambdas.
> 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-10271.



--
This message was sent by Atlassian Jira
(v8.20.7#820007)