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 2023/04/28 18:14:00 UTC

[jira] [Commented] (GROOVY-11014) CompileStatic & Elvis operator: Cannot assign literal value of type (java.lang.Number or java.lang.Comparable) to variable of type double

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

Eric Milles commented on GROOVY-11014:
--------------------------------------

You should be able to write "0" as "0d" or "0.0" or "(double) 0" and it should make the necessary leap.  There are a number of open issues like this where the addition of a ternary puts a level of indirection between source and target.

 

STC tries to blend the two operand types when processing a ternary (elvis or otherwise): https://github.com/apache/groovy/blob/0fbda9b93eb600601dc2dd9b38389ca571310323/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java#L4220

> CompileStatic & Elvis operator: Cannot assign literal value of type (java.lang.Number or java.lang.Comparable) to variable of type double
> -----------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-11014
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11014
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 4.0.10, 4.0.11
>            Reporter: Jason Garrett
>            Priority: Minor
>
> {code:java}
> import groovy.transform.CompileStatic
> @CompileStatic
> class ElvisLiteralAssign {
>     Double x = null
>     double y = x?.doubleValue() ?: 0
> } {code}
> This class fails to compile with error:  [Static type checking] - Cannot assign value of type (java.lang.Number or java.lang.Comparable) to variable of type double
> This affects at least 4.0.10 and 4.0.11, and does not affect 3.0.16 or 3.0.17.



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