groovy

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

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: