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/07/07 19:55:00 UTC
[jira] [Commented] (GROOVY-8965) instanceof with || inserts wrong cast
[ https://issues.apache.org/jira/browse/GROOVY-8965?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17563935#comment-17563935 ]
Eric Milles commented on GROOVY-8965:
-------------------------------------
I have a solution that supports the case where the method ("floatValue" here, "toString" in 10668) is available via the LUB. The duck-typing case (example below) needs handling in method selection and probably dynamic handling in classgen.
{code:groovy}
int cardinality(obj) {
(obj instanceof List || obj instanceof Map) ? obj.size() : 1
}
{code}
> instanceof with || inserts wrong cast
> -------------------------------------
>
> Key: GROOVY-8965
> URL: https://issues.apache.org/jira/browse/GROOVY-8965
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.5.5
> Reporter: Daniil Ovchinnikov
> Assignee: Eric Milles
> Priority: Major
>
> {code:java}
> @groovy.transform.CompileStatic
> def foo(a) {
> if (a instanceof Integer || a instanceof Double) {
> a.floatValue() // expected: cast to Number; actual: cast to Integer
> }
> }
> println foo(1d).class // CCE: java.lang.Double cannot be cast to java.lang.Integer
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)