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

Eric Milles updated GROOVY-5502:
--------------------------------
    Description: 
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.

  was:
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.


> 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
>            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.3.4#803005)

Reply via email to