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

Trait instance init blocks are called only when there are fields defined in same Trait

    XMLWordPrintableJSON

    Details

      Description

      With ref. to GROOVY-8880, it seems Instance Init Blocks in Traits are appended/called only when there are fields defined in the same trait.

      Example:

      trait TraitOne {
          {
              System.out.println("Does not work")
          }
      }
      trait TraitTwo {
          Integer instanceCounter //immutable, non-shareable
          {
              System.out.println("Works")
              instanceCounter = 1
          }
      }
      class ClassWithTrait implements TraitOne, TraitTwo {
      }
      new ClassWithTrait().instanceCounter
      

      Output:

      Works
      

      Expected output (order may differ):

      Works
      Does not work
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                anton.pryamostanov Anton Pryamostanov
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: