Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7015

Groovy static type checking doesn't solve the generic types properly (behavior is different than Java)

    XMLWordPrintableJSON

Details

    Description

      In the following code, the type of myVar variable is of type Base instead of Derived. This behavior is different than Java.

      class Base {}
      class Derived extends Base {
        public void sayHello() {
          System.out.println("hello");
        }
      }
      
      class GBase<T extends Base>{
        T myVar;
      }
      @CompileStatic
      class GDerived extends GBase<Derived>
      {
        GDerived() { myVar = new Derived(); }
        public void method() {
          // Groovy compiler thinks myVar is of type base and doesn't have sayHello
          myVar.sayHello();
        }
      }
      
      public class Main
      {
        public static void main(String[] args)
        {
          GDerived d = new GDerived();
          d.method();
        }
      }
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            harminder Harminder Singh
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: