[ https://issues.apache.org/jira/browse/GROOVY-5502?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Paul King updated GROOVY-5502: ------------------------------ Summary: If/else branch does not always infer the variable type (was: If/else brach does not always infer the variable type) > 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.3.0, 2.4.3 > Reporter: Ariel Morelli Andres > Priority: Minor > > Suppose we have the following code: > {code} > class A { > void mA() { > } > } > class B extends A { > } > class C extends A { > } > @groovy.transform.CompileStatic > 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.mA() //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 (v7.6.3#76005)