Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3080 implementation for inner classes in Groovy
  3. GROOVY-2443

Enum does not support instance (value) specific methods

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.7-rc-1
    • None
    • None

    Description

      Java enum allows instance (value) specific methods. Groovy does not support this currently. The following code fails in Groovy (compilation error):

      class GroovyEnumRangeTest extends GroovyTestCase {
      void testEnumMethod()

      { assertEquals "Work", Day.MONDAY.activity() assertEquals "Relax", Day.SUNDAY.activity() }

      }

      enum Day {
      SUNDAY {
      String activity()

      { 'Relax' }

      }, MONDAY, TUESDAY, WEDNESDAY,
      THURSDAY, FRIDAY, SATURDAY

      String activity()

      { 'Work' }
      }

      If I need it right now, I am able to use EMC to achieve this:

      class GroovyEnumRangeTest extends GroovyTestCase
      {
      void testEnumMethod()
      {
      def emc = new ExpandoMetaClass(Day)
      emc.activity = {-> 'Relax' }
      emc.initialize()

      Day.SUNDAY.metaClass = emc
      assertEquals "Work", Day.MONDAY.activity()
      assertEquals "Relax", Day.SUNDAY.activity()
      assertEquals "Work", Day.TUESDAY.activity()
      }
      }

      enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
      THURSDAY, FRIDAY, SATURDAY

      String activity() { 'Work' }

      }

      so it is not major (IMHO).

      Attachments

        1. GroovyEnumMethods.groovy
          0.3 kB
          Venkat Srinivasan

        Issue Links

          Activity

            People

              blackdrag Jochen Theodorou
              venkats Venkat Srinivasan
              Votes:
              3 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: