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

Problem With CompileStatic And Trait Methods In Abstract Subclass

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 2.4.3
    • None
    • Compiler
    • None

    Description

      This issue was created to attach files to this bug: https://issues.apache.org/jira/browse/GROOVY-7416

      @CompileStatic should compile the following:

      abstract class ABaseClass implements SomeInterface, SomeTrait, TraitWithSomeOtherInterface {
      
      }
      abstract class ASubClass extends ABaseClass {
        void someMethod() { 
          someInterfaceMethod() 
          someTraitMethod() 
          someOtherInterfaceMethod() 
        }
      }
      interface SomeInterface { 
        void someInterfaceMethod() 
      }
      interface SomeOtherInterface { 
        void someOtherInterfaceMethod() 
      }
      trait SomeTrait { 
        abstract void someTraitMethod() 
      }
      trait TraitWithSomeOtherInterface implements SomeOtherInterface {
      
      }
      

      Now that I've had time to think about this some more, the above isn't complete because there's an infinite number of possibilities with traits:

      1) traits can extend other traits. For example,
      trait A extend B {
      }
      trait B extend C {
      }
      ... etc
      1) traits can implement other traits/interfaces. For example,
      trait A implements B, C, D {
      }
      trait B extend E {
      }
      trait C implements F {
      }
      ... and so on

      The main value of traits, aside from (implementation w/ interface) is the behavior of "stackable traits." So as long as we can get that to work, then all
      other permutations aren't as important imho. For example:

      ABaseClass implements TraitA, TraitB, TraitC {
      
      }
      
      ASubClass extends ABaseClass {
         someMethod() {
           doSomething() // this method is equivalent to calling TraitC.super.doSomething(), then TraitB.super.doSomething(), then TraitA.super.doSomething()
        }
      }
      
      interface IDoSomething {
        void doSomething()
      }
      
      trait TraitA implements IDoSomething {
        void doSomething() {
          if (forA) {
            // call for us
          } else {
            // pass the call on
            try { // we wrap this in try/catch because we won't assume the implements order
              super.doSomething() 
            } catch(e) {
            }
          }
        }
      }
      trait TraitB implements IDoSomething {
          if (forB) {
            // call for us
          } else {
            // pass the call on
            try { // we wrap this in try/catch because we won't assume the implements order
              super.doSomething() 
            } catch(e) {
            }
          }
      }
      trait TraitC implements IDoSomething {
          if (forC) {
            // call for us
          } else {
            // pass the call on
            try { // we wrap this in try/catch because we won't assume the implements order
              super.doSomething() 
            } catch(e) {
            }
          }
      }
      

      Attachments

        1. abstractcompilestatic.zip
          53 kB
          Allen Arakaki

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ikakara Allen Arakaki
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: