[
https://issues.apache.org/jira/browse/GROOVY-5502?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Jochen Theodorou updated GROOVY-5502:
-------------------------------------
Affects Version/s: 3.0.19
4.0.14
2.5.23
2.0.0
> If/else branch does not always infer the variable type
> ------------------------------------------------------
>
> Key: GROOVY-5502
> URL: https://issues.apache.org/jira/browse/GROOVY-5502
> Project: Groovy
> Issue Type: Improvement
> Components: Static Type Checker
> Affects Versions: 2.0.0, 2.5.23, 4.0.14, 3.0.19
> Reporter: Ariel Morelli Andres
> Priority: Minor
>
> Suppose we have the following code:
> {code}
> class A {
> void m() {
> }
> }
> class B extends A {
> }
> class C extends A {
> }
> @groovy.transform.TypeChecked
> class Test {
> void m() {
> def var = new Object() //If instead I put just "def var" it works as
> spected
> if (true) {
> var = new B()
> }
> else {
> var = new C()
> }
> var.m() //fails here
> }
> }
> {code}
> In this case, after the if/else structure we can infer that var is instanceOf
> A.
> But, the initialization seems to confuse the type inference.
> [Static type checking] - Cannot find matching method java.lang.Object#mA()
> at line: 23, column: 7
> If instead of {code}def var = new Object(){code} we put just {code}def
> var{code}, then the inference works fine.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)