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

String to Enum coercion

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.7.6, 1.8-beta-3
    • None
    • None
    • Patch

    Description

      The provided patch adds String to Enum coercion. Basically, when trying to affect a string to a variable expecting an enumeration type, Groovy fails with a cast error. The patch allows coercing (G)String to the expected enumeration type. It's useful since it allows avoiding noisy imports when you already know the possible enum values.

      Patch is applicable to both trunk and 1.7.X branch and adds this test in CastTest.groovy :

      void testCastEnum() {
      	CastEnum val;
      	val = 'value1'
      	assert val == val.value1
      	def i=2
      	val =  "value$i"
      	assert val == val.value2
      }
      enum CastEnum {
      	value1,
      	value2
      }
      

      Attachments

        1. String_to_Enum_coercion.patch
          1 kB
          Cédric Champeau

        Activity

          People

            guillaume Guillaume Sauthier
            melix Cédric Champeau
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: