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)

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    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

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

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

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment