groovy
  1. groovy
  2. GROOVY-2210

The behavior of enum is different between Java and Groovy in switch statement.

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 1.1-rc-1
    • Fix Version/s: 1.1-rc-2
    • Component/s: None
    • Labels:
      None
    • Environment:
      Windows 2000
      Groovy Version: 1.1-rc-1
      JVM: 1.6.0_02-b05
    • Number of attachments :
      0

      Description

      The following code snippet can not be run, but after I used the full name such as Fruit.APPLE, Fruit.ORANGE, Fruit.BANANA, the code can be run successfully.

      public enum Fruit {
      	APPLE, ORANGE, BANANA
      }
      
      Fruit f = Fruit.BANANA
      println f
      switch (f) {
      	case APPLE:
      	  println "Apple"
      	  break
      	case ORANGE:
      	  println "Orange"
      	  break
      	case BANANA:
      	  println "Banana"
      	  break
      	default:
      	  println "Will not reach!"
      	  break  
      }
      

        Activity

        Hide
        Paul King added a comment -

        Indeed. Not only does Java allow the class name to be dropped, it requires it:

        Temp.java:22: an enum switch case label must be the unqualified name of an enumeration constant
            case Fruit.ORANGE:
                      ^
        
        Show
        Paul King added a comment - Indeed. Not only does Java allow the class name to be dropped, it requires it: Temp.java:22: an enum switch case label must be the unqualified name of an enumeration constant case Fruit.ORANGE: ^
        Hide
        blackdrag blackdrag added a comment -

        for now we require the class name in Groovy, because enums are not imported by default. It would differ to much from java. You can have the switch-case you showed if you add a static import like "import static Fruit.*". I guess that additional line is not too much of a problem

        Show
        blackdrag blackdrag added a comment - for now we require the class name in Groovy, because enums are not imported by default. It would differ to much from java. You can have the switch-case you showed if you add a static import like "import static Fruit.*". I guess that additional line is not too much of a problem
        Hide
        Mauro Molinari added a comment -

        I think this should be at least reconsidered.

        First of all, the Groovy documentation neither at:
        http://groovy.codehaus.org/Using+Enums
        nor at:
        http://groovy.codehaus.org/Differences+from+Java
        even mention this difference from Java.

        Secondly, usually a Java source file is usually a well-formed Groovy source file, too, but this is the first real exception I found. It's even more important because, as Paul King noted, the syntax required by Groovy is incompatible with that required by Java (it's not just a restriction on what Java accepts).

        Show
        Mauro Molinari added a comment - I think this should be at least reconsidered. First of all, the Groovy documentation neither at: http://groovy.codehaus.org/Using+Enums nor at: http://groovy.codehaus.org/Differences+from+Java even mention this difference from Java. Secondly, usually a Java source file is usually a well-formed Groovy source file, too, but this is the first real exception I found. It's even more important because, as Paul King noted, the syntax required by Groovy is incompatible with that required by Java (it's not just a restriction on what Java accepts).

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Tiger Shark
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: